From 693add2f21b6f78d971edf8cda4572081188d4dc Mon Sep 17 00:00:00 2001 From: Eugene Ivanov <93479789+evgeek@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:36:27 +0300 Subject: [PATCH] Fix query params parsing (#45) --- src/Services/Url.php | 16 +++++++++++++++- tests/Unit/Services/UrlTest.php | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Services/Url.php b/src/Services/Url.php index 0a36e125..47e44ad4 100644 --- a/src/Services/Url.php +++ b/src/Services/Url.php @@ -42,7 +42,7 @@ public static function parsePathAndParams(string $url): array $params = []; $paramsString = parse_url($url)['query'] ?? null; if ($paramsString) { - parse_str($paramsString, $params); + $params = self::parseStr($paramsString); $pathString = str_replace("?$paramsString", '', $pathString); } @@ -79,4 +79,18 @@ private static function prepareQueryParams(array $params): string return $paramsString === '' ? '' : "?$paramsString"; } + + /** @return array */ + private static function parseStr(string $query): array + { + $data = preg_replace_callback( + '/(?:^|(?<=&))[^=[]+/', + static fn ($match) => bin2hex(urldecode($match[0])), + $query + ); + + parse_str($data, $values); + + return array_combine(array_map('hex2bin', array_keys($values)), $values); + } } diff --git a/tests/Unit/Services/UrlTest.php b/tests/Unit/Services/UrlTest.php index c50835ab..cfe3063b 100644 --- a/tests/Unit/Services/UrlTest.php +++ b/tests/Unit/Services/UrlTest.php @@ -131,6 +131,17 @@ public static function urlForParsingDataProvider(): array 'filter' => 'aa=bb;cc!=d\;d', ], ], + [ + URL::API . '/segment5/segment6?operation.id=a&b = c', + [ + 'segment5', + 'segment6', + ], + [ + 'operation.id' => 'a', + 'b ' => ' c', + ], + ], ]; }