diff --git a/src/QueryParameterBag.php b/src/QueryParameterBag.php index b9a6f9f..311944d 100644 --- a/src/QueryParameterBag.php +++ b/src/QueryParameterBag.php @@ -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; diff --git a/src/Url.php b/src/Url.php index d111fc9..ed1b2a8 100644 --- a/src/Url.php +++ b/src/Url.php @@ -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; diff --git a/tests/QueryParameterBagTest.php b/tests/QueryParameterBagTest.php index 4372673..6607ba4 100644 --- a/tests/QueryParameterBagTest.php +++ b/tests/QueryParameterBagTest.php @@ -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([]); +}); diff --git a/tests/UrlQueryParametersTest.php b/tests/UrlQueryParametersTest.php index ede6425..d80d983 100644 --- a/tests/UrlQueryParametersTest.php +++ b/tests/UrlQueryParametersTest.php @@ -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');