From 4e71cae0c942fe1ce2ae22506bd46ecdfa5b4f0e Mon Sep 17 00:00:00 2001 From: otsch Date: Wed, 12 Apr 2023 23:30:44 +0200 Subject: [PATCH] Fix bug introduced in v1.0.2 Fixes an issue introduced in v1.0.2. When creating an instance from string, it's containing an array and the brackets are encoded (like `filter%5Bfoo%5D%5B%5D=1&filter%5Bfoo%5D%5B%5D=2`), the method trying to fix duplicate keys without brackets added another array level (like `filter[foo][]` to `filter[foo][][]`). This is fixed with this release. --- CHANGELOG.md | 4 ++++ src/Query.php | 2 ++ tests/FromStringTest.php | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c766596..f9ece94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.0.3] - 2023-04-12 +### Fixed +- Fixes an issue introduced in v1.0.2. When creating an instance from string, it's containing an array and the brackets are encoded (like `filter%5Bfoo%5D%5B%5D=1&filter%5Bfoo%5D%5B%5D=2`), the method trying to fix duplicate keys without brackets added another array level (like `filter[foo][]` to `filter[foo][][]`). This is fixed with this release. + ## [1.0.2] - 2023-04-12 ### Fixed - Duplicate keys without array notation (like `foo=bar&foo=baz`) are now also interpreted as array (like `foo[]=bar&foo[]=baz`). This is considered a bugfix because: diff --git a/src/Query.php b/src/Query.php index 55ca894..5e9ac46 100644 --- a/src/Query.php +++ b/src/Query.php @@ -1019,6 +1019,8 @@ private function fixDuplicateKeysInString(string $query): string $keyOccurrences = array_count_values(array_map(fn ($val) => explode('=', $val, 2)[0], explode('&', $query))); foreach ($keyOccurrences as $key => $count) { + $key = $this->toStringWithUnencodedBrackets($key); + if ($count > 1 && !str_contains($key, '[')) { // Duplicate query string key without array notation, convert to {keyName}[] structure $query = preg_replace( diff --git a/tests/FromStringTest.php b/tests/FromStringTest.php index 580eee4..6770c54 100644 --- a/tests/FromStringTest.php +++ b/tests/FromStringTest.php @@ -186,3 +186,15 @@ function (array $array, string $normalized, string $raw) { 'test2' => 2, ]); }); + +it('correctly parses array syntax when brackets are encoded', function () { + expect( + Query::fromString( + 'filter%5Bdestination%5D%5B%5D=101&filter%5Bdestination%5D%5B%5D=103&filter%5Bdestination%5D%5B%5D=106' + )->toArray() + )->toBe([ + 'filter' => [ + 'destination' => ['101', '103', '106'] + ] + ]); +});