File tree Expand file tree Collapse file tree 4 files changed +62
-0
lines changed Expand file tree Collapse file tree 4 files changed +62
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 *********************************/
Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff line change @@ -230,6 +230,22 @@ echo $modifier->getUri()->getQuery(); //display "kingkong=toto&fo.o=bar&fo_o=bar
230230echo $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 >
You can’t perform that action at this time.
0 commit comments