Skip to content

Commit

Permalink
fix ValueAs:arr - parse_str does key mangling we want to avoid (#25)
Browse files Browse the repository at this point in the history
* fix ValueAs:arr - parse_str does key mangling we want to avoid

* support missing value

* regexp is surprisingly much faster
  • Loading branch information
TomK authored Jun 14, 2021
1 parent 33906e3 commit 8b4cf5a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ValueAs.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ public static function arr($value, $default = [])
if(strpos($value, '=') !== false)
{
$array = [];
parse_str($value, $array);
preg_match_all('/([^=&]+)(?:=)?([^&]+)?(?:&)?/', $value, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
foreach($matches as $match)
{
$array[$match[1]] = $match[2] ?: '';
}
return $array;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/ValueAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public function exactProvider()
['arr', "hey", null, ["hey"]],
['arr', "hello,world", null, ["hello", "world"]],
['arr', "test=one&unit=two", null, ["test" => 'one', "unit" => 'two']],
['arr', "test.one=one.test", null, ["test.one" => 'one.test']],
['arr', "test=one", null, ["test" => 'one']],
['arr', "hello&world=two", null, ['hello' => '', 'world' => 'two']],
['arr', "", ["test"], ["test"]],
['arr', tmpfile(), ["test"], ["test"]],
['arr', $objectTest, ["test"], ["item" => "value"]],
Expand Down

0 comments on commit 8b4cf5a

Please sign in to comment.