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 05c79f0 commit 189b460
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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 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 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

0 comments on commit 189b460

Please sign in to comment.