Skip to content

Commit 8b4cf5a

Browse files
authored
fix ValueAs:arr - parse_str does key mangling we want to avoid (#25)
* fix ValueAs:arr - parse_str does key mangling we want to avoid * support missing value * regexp is surprisingly much faster
1 parent 33906e3 commit 8b4cf5a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/ValueAs.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ public static function arr($value, $default = [])
161161
if(strpos($value, '=') !== false)
162162
{
163163
$array = [];
164-
parse_str($value, $array);
164+
preg_match_all('/([^=&]+)(?:=)?([^&]+)?(?:&)?/', $value, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
165+
foreach($matches as $match)
166+
{
167+
$array[$match[1]] = $match[2] ?: '';
168+
}
165169
return $array;
166170
}
167171

tests/ValueAsTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public function exactProvider()
7373
['arr', "hey", null, ["hey"]],
7474
['arr', "hello,world", null, ["hello", "world"]],
7575
['arr', "test=one&unit=two", null, ["test" => 'one', "unit" => 'two']],
76+
['arr', "test.one=one.test", null, ["test.one" => 'one.test']],
7677
['arr', "test=one", null, ["test" => 'one']],
78+
['arr', "hello&world=two", null, ['hello' => '', 'world' => 'two']],
7779
['arr', "", ["test"], ["test"]],
7880
['arr', tmpfile(), ["test"], ["test"]],
7981
['arr', $objectTest, ["test"], ["item" => "value"]],

0 commit comments

Comments
 (0)