From 1d4aceb2234bbab3139d106ce32b8bf68a0dd1ed Mon Sep 17 00:00:00 2001 From: Martin Brecht-Precht Date: Mon, 19 Dec 2016 17:21:36 +0100 Subject: [PATCH] Added hasQueryParameterWithKey($key) method. --- src/Url.php | 19 +++++++++++++++++++ test/UrlTest.php | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Url.php b/src/Url.php index a772a58..c6de1cf 100644 --- a/src/Url.php +++ b/src/Url.php @@ -381,6 +381,25 @@ public function removeQueryParameterByKey($key) return $this; } + /** + * @param string $key + * @return bool + */ + public function hasQueryParameterWithKey($key) + { + if (!is_string($key)) { + $argumentType = (is_object($key)) ? get_class($key) : gettype($key); + throw new \InvalidArgumentException('Expected query parameter key as string; got ' . $argumentType); + } + $queryParameterCount = $this->countQueryParameters(); + for ($i = 0; $i < $queryParameterCount; $i++) { + if ($this->queryParameters[$i]->getKey() === $key) { + return true; + } + } + return false; + } + /** * @return $this */ diff --git a/test/UrlTest.php b/test/UrlTest.php index 53ba3f3..b2c5369 100644 --- a/test/UrlTest.php +++ b/test/UrlTest.php @@ -98,6 +98,13 @@ public function testInvalidArgument15() $url->setFragment(array()); } + public function testInvalidArgument16() + { + $this->setExpectedException(get_class(new \InvalidArgumentException())); + $url = new Url('https://john:secret@mydomain.com:8443/path/to/resource'); + $url->hasQueryParameterWithKey(array()); + } + public function testParser() { $url = new Url('https://john:secret@mydomain.com:8443/path/to/resource'); @@ -108,6 +115,7 @@ public function testParser() $this->assertTrue($url->hasScheme()); $this->assertTrue($url->hasHostname()); $this->assertTrue($url->hasPath()); + $this->assertTrue($url->hasQueryParameterWithKey('arg1')); $this->assertEquals(2, $url->countQueryParameters());