Skip to content

Commit

Permalink
Merge pull request #56 from MatusBoa/main
Browse files Browse the repository at this point in the history
Add withoutQueryParameters & unsetAll method
  • Loading branch information
freekmurze authored Jul 29, 2022
2 parents 8369362 + 6555aaf commit a055dc7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/QueryParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public function unset(string $key): self
return $this;
}

public function unsetAll(): self
{
$this->parameters = [];

return $this;
}

public function all(): array
{
return $this->parameters;
Expand Down
8 changes: 8 additions & 0 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ public function withoutQueryParameter(string $key): static
return $url;
}

public function withoutQueryParameters(): static
{
$url = clone $this;
$url->query->unsetAll();

return $url;
}

public function getFragment(): string
{
return $this->fragment;
Expand Down
8 changes: 8 additions & 0 deletions tests/QueryParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,11 @@

expect($queryParameterBag)->__toString()->toEqual('category=storage%20furniture&discount=%3E40%25%20off&range%5B0%5D=10&range%5B1%5D=20');
});

it('unsets all query parameters', function () {
$queryParameterBag = QueryParameterBag::fromString(
'category=storage%20furniture&discount=%3E40%25%20off&range%5B0%5D=10&range%5B1%5D=20'
)->unsetAll();

expect($queryParameterBag)->all()->toEqual([]);
});
13 changes: 13 additions & 0 deletions tests/UrlQueryParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@
expect($url)->hasQueryParameter('offset')->toBeFalse();
});

it('can unset all query parameters', function () {
$url = Url::create()
->withQuery('offset=10')
->withoutQueryParameters();

expect($url)->getAllQueryParameters()->toEqual([]);

$url = Url::fromString('https://example.com?foo=bar')
->withoutQueryParameters();

expect((string) $url)->toEqual('https://example.com');
});


it('can handle empty query parameters', function () {
$url = Url::create()->withQuery('offset');
Expand Down

0 comments on commit a055dc7

Please sign in to comment.