Skip to content

Commit 0cd97af

Browse files
committed
Adding Modifier::removeQueryParameterIndices
1 parent ec0ec4c commit 0cd97af

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

components/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

components/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
*********************************/

components/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
*/

docs/components/7.0/modifiers.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ echo $modifier->getUri()->getQuery(); //display "kingkong=toto&fo.o=bar&fo_o=bar
230230
echo $newUri->getUri()->getQuery(); //display "kingkong=toto&fo_o=bar"
231231
~~~
232232

233+
234+
### Modifier::removeQueryParameterIndices
235+
236+
<p class="message-notice">since version <code>7.2.0</code></p>
237+
238+
Removes query params numeric indices from the current URI query string. The removal preserves mangled key params.
239+
240+
~~~php
241+
$uri = "http://example.com/test.php?kingkong[1]=toto&fkingkong[2]=toto";
242+
$modifier = Modifier::from($uri);
243+
$newUri = $modifier->removeQueryParameterIndices();
244+
245+
echo $modifier->getUri()->getQuery(); //display "kingkong%5B1%5D=toto&fkingkong%5B2%5D=toto"
246+
echo $newUri->getUri()->getQuery(); //display "kingkong%5B%5D=toto&fkingkong%5B%5D=toto"
247+
~~~
248+
233249
### Modifier::mergeQueryParameters
234250

235251
<p class="message-notice">since version <code>7.2.0</code></p>

0 commit comments

Comments
 (0)