Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
typo3
qfq
Commits
8fc4e2a5
Commit
8fc4e2a5
authored
Jan 22, 2016
by
Carsten Rose
Browse files
KeyValueStringParser: parse() extended. Added Mode to duplicate keys values to values column
parent
26beab71
Changes
2
Hide whitespace changes
Inline
Side-by-side
qfq/helper/KeyValueStringParser.php
View file @
8fc4e2a5
...
...
@@ -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 {
}
/**
* Parse
s
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
*
* @return array associative array indexed by keys
* @throws \qfq\exceptions\UserException
* @param string $keyValueDelimiter
* @param string $listDelimiter
* @param string $keyValueMode
* @return array associative array indexed by keys
* @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
;
}
}
...
...
tests/phpunit/KeyValueStringParserTest.php
View file @
8fc4e2a5
...
...
@@ -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
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment