Skip to content

Commit

Permalink
Adding Modifier::removeQueryParameterIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 30, 2023
1 parent ec0ec4c commit 0cd97af
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions components/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*********************************/
Expand Down
26 changes: 26 additions & 0 deletions components/ModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
16 changes: 16 additions & 0 deletions docs/components/7.0/modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 0cd97af

Please sign in to comment.