Skip to content

Commit 4e71cae

Browse files
committed
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.
1 parent 9ae249c commit 4e71cae

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.0.3] - 2023-04-12
11+
### Fixed
12+
- 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.
13+
1014
## [1.0.2] - 2023-04-12
1115
### Fixed
1216
- 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:

src/Query.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,8 @@ private function fixDuplicateKeysInString(string $query): string
10191019
$keyOccurrences = array_count_values(array_map(fn ($val) => explode('=', $val, 2)[0], explode('&', $query)));
10201020

10211021
foreach ($keyOccurrences as $key => $count) {
1022+
$key = $this->toStringWithUnencodedBrackets($key);
1023+
10221024
if ($count > 1 && !str_contains($key, '[')) {
10231025
// Duplicate query string key without array notation, convert to {keyName}[] structure
10241026
$query = preg_replace(

tests/FromStringTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,15 @@ function (array $array, string $normalized, string $raw) {
186186
'test2' => 2,
187187
]);
188188
});
189+
190+
it('correctly parses array syntax when brackets are encoded', function () {
191+
expect(
192+
Query::fromString(
193+
'filter%5Bdestination%5D%5B%5D=101&filter%5Bdestination%5D%5B%5D=103&filter%5Bdestination%5D%5B%5D=106'
194+
)->toArray()
195+
)->toBe([
196+
'filter' => [
197+
'destination' => ['101', '103', '106']
198+
]
199+
]);
200+
});

0 commit comments

Comments
 (0)