From fdba41004d6842926ca54271227f1ca537f8b9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=BA=C5=A1=20Koterba?= Date: Tue, 26 Jul 2022 22:04:34 +0200 Subject: [PATCH 1/2] Add withoutQueryParameters & unsetAll method --- src/QueryParameterBag.php | 7 +++++++ src/Url.php | 8 ++++++++ tests/QueryParameterBagTest.php | 8 ++++++++ tests/UrlQueryParametersTest.php | 8 ++++++++ 4 files changed, 31 insertions(+) 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..7d8f17f 100644 --- a/tests/UrlQueryParametersTest.php +++ b/tests/UrlQueryParametersTest.php @@ -69,6 +69,14 @@ expect($url)->hasQueryParameter('offset')->toBeFalse(); }); +it('can unset all query parameters', function () { + $url = Url::create() + ->withQuery('offset=10') + ->withoutQueryParameters(); + + expect($url)->getAllQueryParameters()->toEqual([]); +}); + it('can handle empty query parameters', function () { $url = Url::create()->withQuery('offset'); From 6555aaffedd97e4f4c577de0746c4ac7c2334b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=BA=C5=A1=20Koterba?= Date: Tue, 26 Jul 2022 22:08:47 +0200 Subject: [PATCH 2/2] Add one more test --- tests/UrlQueryParametersTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/UrlQueryParametersTest.php b/tests/UrlQueryParametersTest.php index 7d8f17f..d80d983 100644 --- a/tests/UrlQueryParametersTest.php +++ b/tests/UrlQueryParametersTest.php @@ -75,6 +75,11 @@ ->withoutQueryParameters(); expect($url)->getAllQueryParameters()->toEqual([]); + + $url = Url::fromString('https://example.com?foo=bar') + ->withoutQueryParameters(); + + expect((string) $url)->toEqual('https://example.com'); });