From 8b4cf5afd03deb5d0b9866bb55316e9eda239522 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Mon, 14 Jun 2021 13:30:13 +0100 Subject: [PATCH] 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 --- src/ValueAs.php | 6 +++++- tests/ValueAsTest.php | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ValueAs.php b/src/ValueAs.php index 2ce5df0..b0c4eff 100644 --- a/src/ValueAs.php +++ b/src/ValueAs.php @@ -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; } diff --git a/tests/ValueAsTest.php b/tests/ValueAsTest.php index 397200f..5f84b52 100644 --- a/tests/ValueAsTest.php +++ b/tests/ValueAsTest.php @@ -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"]],