From bb2e6631645e5ea0cb10b1296b2f8c1ac213cd23 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Mon, 14 Jun 2021 11:54:13 +0100 Subject: [PATCH 1/3] fix ValueAs:arr - parse_str does key mangling we want to avoid --- src/ValueAs.php | 6 +++++- tests/ValueAsTest.php | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ValueAs.php b/src/ValueAs.php index 2ce5df0..017cb83 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); + foreach(explode('&', $value) as $pair) + { + [$key, $val] = explode('=', $pair); + $array[$key] = $val; + } return $array; } diff --git a/tests/ValueAsTest.php b/tests/ValueAsTest.php index 397200f..2ef734b 100644 --- a/tests/ValueAsTest.php +++ b/tests/ValueAsTest.php @@ -73,6 +73,7 @@ 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', "", ["test"], ["test"]], ['arr', tmpfile(), ["test"], ["test"]], From ab872f5ebd4232e4d23a56682db3fc3c84864b12 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Mon, 14 Jun 2021 12:01:38 +0100 Subject: [PATCH 2/3] support missing value --- src/ValueAs.php | 2 +- tests/ValueAsTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ValueAs.php b/src/ValueAs.php index 017cb83..d238964 100644 --- a/src/ValueAs.php +++ b/src/ValueAs.php @@ -163,7 +163,7 @@ public static function arr($value, $default = []) $array = []; foreach(explode('&', $value) as $pair) { - [$key, $val] = explode('=', $pair); + [$key, $val] = array_pad(explode('=', $pair, 2), 2, ''); $array[$key] = $val; } return $array; diff --git a/tests/ValueAsTest.php b/tests/ValueAsTest.php index 2ef734b..5f84b52 100644 --- a/tests/ValueAsTest.php +++ b/tests/ValueAsTest.php @@ -75,6 +75,7 @@ public function exactProvider() ['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"]], From 19e3b687499e16c431e2da0e882328c07ce54ac8 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Mon, 14 Jun 2021 13:24:03 +0100 Subject: [PATCH 3/3] regexp is surprisingly much faster --- src/ValueAs.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ValueAs.php b/src/ValueAs.php index d238964..b0c4eff 100644 --- a/src/ValueAs.php +++ b/src/ValueAs.php @@ -161,10 +161,10 @@ public static function arr($value, $default = []) if(strpos($value, '=') !== false) { $array = []; - foreach(explode('&', $value) as $pair) + preg_match_all('/([^=&]+)(?:=)?([^&]+)?(?:&)?/', $value, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL); + foreach($matches as $match) { - [$key, $val] = array_pad(explode('=', $pair, 2), 2, ''); - $array[$key] = $val; + $array[$match[1]] = $match[2] ?: ''; } return $array; }