Skip to content

Commit

Permalink
Fix query params parsing (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeek authored Jan 10, 2024
1 parent 80d2953 commit 693add2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Services/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -79,4 +79,18 @@ private static function prepareQueryParams(array $params): string

return $paramsString === '' ? '' : "?$paramsString";
}

/** @return array<int|string, string> */
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);
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Services/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
];
}

Expand Down

0 comments on commit 693add2

Please sign in to comment.