From 0cd97afa10abe11050a5ce11eb91d8a7f228d242 Mon Sep 17 00:00:00 2001
From: ignace nyamagana butera <nyamsprod@gmail.com>
Date: Wed, 30 Aug 2023 22:17:27 +0200
Subject: [PATCH] Adding Modifier::removeQueryParameterIndices

---
 components/CHANGELOG.md          |  1 +
 components/Modifier.php          | 19 +++++++++++++++++++
 components/ModifierTest.php      | 26 ++++++++++++++++++++++++++
 docs/components/7.0/modifiers.md | 16 ++++++++++++++++
 4 files changed, 62 insertions(+)

diff --git a/components/CHANGELOG.md b/components/CHANGELOG.md
index 105485f1..64dd8f0c 100644
--- a/components/CHANGELOG.md
+++ b/components/CHANGELOG.md
@@ -13,6 +13,7 @@ All Notable changes to `League\Uri\Components` will be documented in this file
 - `Modifier::appendQueryParameters`
 - `Modifier::mergeQueryParameters`
 - `Modifier::removeQueryParameters`
+- `Modifier::removeQueryParametersIndices`
 
 ### Fixed
 
diff --git a/components/Modifier.php b/components/Modifier.php
index 12dd49eb..9ddc4675 100644
--- a/components/Modifier.php
+++ b/components/Modifier.php
@@ -250,6 +250,25 @@ public function removeEmptyQueryPairs(): static
         ));
     }
 
+    /**
+     * Returns an instance where numeric indices associated to PHP's array like key are removed.
+     *
+     * This method MUST retain the state of the current instance, and return
+     * an instance that contains the query component normalized so that numeric indexes
+     * are removed from the pair key value.
+     *
+     * ie.: toto[3]=bar[3]&foo=bar becomes toto[]=bar[3]&foo=bar
+     */
+    public function removeQueryParameterIndices(): static
+    {
+        return new static($this->uri->withQuery(
+            static::normalizeComponent(
+                Query::fromUri($this->uri)->withoutNumericIndices()->value(),
+                $this->uri
+            )
+        ));
+    }
+
     /*********************************
      * Host modifier methods
      *********************************/
diff --git a/components/ModifierTest.php b/components/ModifierTest.php
index b47dae18..3e9aa1ae 100644
--- a/components/ModifierTest.php
+++ b/components/ModifierTest.php
@@ -212,6 +212,32 @@ public static function removeParamsProvider(): array
         ];
     }
 
+    /**
+     * @dataProvider removeQueryParameterIndicesProvider
+     */
+    public function testWithoutQueryParameterIndices(string $uri, string $expected): void
+    {
+        self::assertSame($expected, Modifier::from($uri)->removeQueryParameterIndices()->getUri()->getQuery());
+    }
+
+    public static function removeQueryParameterIndicesProvider(): array
+    {
+        return [
+            [
+                'uri' => 'http://example.com?foo=bar',
+                'expected' => 'foo=bar',
+            ],
+            [
+                'uri' => 'http://example.com?foo[0]=bar&foo[1]=baz',
+                'expected' => 'foo%5B%5D=bar&foo%5B%5D=baz',
+            ],
+            [
+                'uri' => 'http://example.com?foo[not-remove]=bar&foo[1]=baz',
+                'expected' => 'foo%5Bnot-remove%5D=bar&foo%5B%5D=baz',
+            ],
+        ];
+    }
+
     /**
      * @dataProvider removeEmptyPairsProvider
      */
diff --git a/docs/components/7.0/modifiers.md b/docs/components/7.0/modifiers.md
index cc1c72d9..22d1320d 100644
--- a/docs/components/7.0/modifiers.md
+++ b/docs/components/7.0/modifiers.md
@@ -230,6 +230,22 @@ echo $modifier->getUri()->getQuery(); //display "kingkong=toto&fo.o=bar&fo_o=bar
 echo $newUri->getUri()->getQuery();   //display "kingkong=toto&fo_o=bar"
 ~~~
 
+
+### Modifier::removeQueryParameterIndices
+
+<p class="message-notice">since version <code>7.2.0</code></p>
+
+Removes query params numeric indices from the current URI query string. The removal preserves mangled key params.
+
+~~~php
+$uri = "http://example.com/test.php?kingkong[1]=toto&fkingkong[2]=toto";
+$modifier = Modifier::from($uri);
+$newUri = $modifier->removeQueryParameterIndices();
+
+echo $modifier->getUri()->getQuery(); //display "kingkong%5B1%5D=toto&fkingkong%5B2%5D=toto"
+echo $newUri->getUri()->getQuery();   //display "kingkong%5B%5D=toto&fkingkong%5B%5D=toto"
+~~~
+
 ### Modifier::mergeQueryParameters
 
 <p class="message-notice">since version <code>7.2.0</code></p>