Commit 8fc4e2a5 authored by Carsten  Rose's avatar Carsten Rose
Browse files

KeyValueStringParser: parse() extended. Added Mode to duplicate keys values to values column

parent 26beab71
......@@ -14,6 +14,7 @@ use qfq;
use qfq\exceptions\UserException;
require_once(__DIR__ . '/../exceptions/UserException.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
/**
* Class KeyValueStringParser
......@@ -73,18 +74,23 @@ class KeyValueStringParser {
}
/**
* Parses key/value pairs string.
* Parse key/value pairs string and returns them as an assoc array
*
* Valid: "a:1,b:2,c:,d", "," (empty key AND empty value)
* Invalid: ":" ((empty key forbidden)
* $keyValueMode:
* KEY_VALUE_PAIR: default. If only a key is given, the value is ''. E.G. 'a,b' >> [ 'a' => '', 'b' => '' ].
* KEY_VALUE_SINGLE: If only a key is given, the value is the same as the key. E.G. 'a,b' >> [ 'a' => 'a', 'b' => 'b' ].
*
* @param string $keyValueString string of key/value pairs
*
* @param string $keyValueDelimiter
* @param string $listDelimiter
* @param string $keyValueMode
* @return array associative array indexed by keys
* @throws \qfq\exceptions\UserException
* @throws UserException
*/
public static function parse($keyValueString, $keyValueDelimiter = ":", $listDelimiter = ",") {
public static function parse($keyValueString, $keyValueDelimiter = ":", $listDelimiter = ",", $keyValueMode = KEY_VALUE_PAIR) {
if ($keyValueString === "") {
return array();
}
......@@ -114,12 +120,10 @@ class KeyValueStringParser {
if (count($keyValueArray) === 2) {
// "a:1", "a:"
$returnValue[$key] = self::removeSourroundingQuotes(
trim($keyValueArray[1])
);
$returnValue[$key] = self::removeSourroundingQuotes(trim($keyValueArray[1]));
} else {
// no Value given: "a"
$returnValue[$key] = "";
$returnValue[$key] = ($keyValueMode === KEY_VALUE_PAIR) ? "" : $key;
}
}
......
......@@ -6,6 +6,9 @@
namespace qfq;
use qfq;
require_once(__DIR__ . '/../../qfq/Constants.php');
//use qfq\helper;
use qfq\helper\KeyValueStringParser;
......@@ -128,4 +131,44 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals('value2', $actual['key2']);
}
public function testParseKeyValueSingle() {
$actual = keyValueStringParser::parse('value1,value2', ':', ',', KEY_VALUE_SINGLE);
$expected = [
'value1' => 'value1',
'value2' => 'value2',
];
$this->assertEquals($expected, $actual);
$this->assertCount(2, $actual);
$actual = keyValueStringParser::parse('key1:value1,key2:value2', ':', ',', KEY_VALUE_SINGLE);
$expected = [
'key1' => 'value1',
'key2' => 'value2',
];
$this->assertEquals($expected, $actual);
$this->assertCount(2, $actual);
$actual = keyValueStringParser::parse('key1:value1,key2:value2', ':', ',', KEY_VALUE_PAIR);
$expected = [
'key1' => 'value1',
'key2' => 'value2',
];
$this->assertEquals($expected, $actual);
$this->assertCount(2, $actual);
$actual = keyValueStringParser::parse('value1,value2', ':', ',', KEY_VALUE_PAIR);
$expected = [
'value1' => '',
'value2' => '',
];
$this->assertEquals($expected, $actual);
$this->assertCount(2, $actual);
}
public function testParseDefaults() {
$actual = keyValueStringParser::parse("key1=value1,key2=value2");
$expected = keyValueStringParser::parse("key1=value1,key2=value2", ":", ",");
$this->assertEquals($expected, $actual);
$this->assertCount(2, $actual);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment