Skip to content

Commit

Permalink
Added hasQueryParameterWithKey($key) method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Brecht-Precht committed Dec 19, 2016
1 parent 2ce8fdc commit 1d4aceb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 8 additions & 0 deletions test/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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());

Expand Down

0 comments on commit 1d4aceb

Please sign in to comment.