Skip to content

Commit 189b460

Browse files
committed
Adding Modifier::removeQueryParameterIndices
1 parent 05c79f0 commit 189b460

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All Notable changes to `League\Uri\Components` will be documented in this file
1313
- `Modifier::appendQueryParameters`
1414
- `Modifier::mergeQueryParameters`
1515
- `Modifier::removeQueryParameters`
16+
- `Modifier::removeQueryParametersIndices`
1617

1718
### Fixed
1819

Modifier.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ public function removeEmptyQueryPairs(): static
250250
));
251251
}
252252

253+
/**
254+
* Returns an instance where numeric indices associated to PHP's array like key are removed.
255+
*
256+
* This method MUST retain the state of the current instance, and return
257+
* an instance that contains the query component normalized so that numeric indexes
258+
* are removed from the pair key value.
259+
*
260+
* ie.: toto[3]=bar[3]&foo=bar becomes toto[]=bar[3]&foo=bar
261+
*/
262+
public function removeQueryParameterIndices(): static
263+
{
264+
return new static($this->uri->withQuery(
265+
static::normalizeComponent(
266+
Query::fromUri($this->uri)->withoutNumericIndices()->value(),
267+
$this->uri
268+
)
269+
));
270+
}
271+
253272
/*********************************
254273
* Host modifier methods
255274
*********************************/

ModifierTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,32 @@ public static function removeParamsProvider(): array
212212
];
213213
}
214214

215+
/**
216+
* @dataProvider removeQueryParameterIndicesProvider
217+
*/
218+
public function testWithoutQueryParameterIndices(string $uri, string $expected): void
219+
{
220+
self::assertSame($expected, Modifier::from($uri)->removeQueryParameterIndices()->getUri()->getQuery());
221+
}
222+
223+
public static function removeQueryParameterIndicesProvider(): array
224+
{
225+
return [
226+
[
227+
'uri' => 'http://example.com?foo=bar',
228+
'expected' => 'foo=bar',
229+
],
230+
[
231+
'uri' => 'http://example.com?foo[0]=bar&foo[1]=baz',
232+
'expected' => 'foo%5B%5D=bar&foo%5B%5D=baz',
233+
],
234+
[
235+
'uri' => 'http://example.com?foo[not-remove]=bar&foo[1]=baz',
236+
'expected' => 'foo%5Bnot-remove%5D=bar&foo%5B%5D=baz',
237+
],
238+
];
239+
}
240+
215241
/**
216242
* @dataProvider removeEmptyPairsProvider
217243
*/

0 commit comments

Comments
 (0)