Skip to content

Commit 1d4aceb

Browse files
author
Martin Brecht-Precht
committed
Added hasQueryParameterWithKey($key) method.
1 parent 2ce8fdc commit 1d4aceb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Url.php

+19
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,25 @@ public function removeQueryParameterByKey($key)
381381
return $this;
382382
}
383383

384+
/**
385+
* @param string $key
386+
* @return bool
387+
*/
388+
public function hasQueryParameterWithKey($key)
389+
{
390+
if (!is_string($key)) {
391+
$argumentType = (is_object($key)) ? get_class($key) : gettype($key);
392+
throw new \InvalidArgumentException('Expected query parameter key as string; got ' . $argumentType);
393+
}
394+
$queryParameterCount = $this->countQueryParameters();
395+
for ($i = 0; $i < $queryParameterCount; $i++) {
396+
if ($this->queryParameters[$i]->getKey() === $key) {
397+
return true;
398+
}
399+
}
400+
return false;
401+
}
402+
384403
/**
385404
* @return $this
386405
*/

test/UrlTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public function testInvalidArgument15()
9898
$url->setFragment(array());
9999
}
100100

101+
public function testInvalidArgument16()
102+
{
103+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
104+
$url = new Url('https://john:[email protected]:8443/path/to/resource');
105+
$url->hasQueryParameterWithKey(array());
106+
}
107+
101108
public function testParser()
102109
{
103110
$url = new Url('https://john:[email protected]:8443/path/to/resource');
@@ -108,6 +115,7 @@ public function testParser()
108115
$this->assertTrue($url->hasScheme());
109116
$this->assertTrue($url->hasHostname());
110117
$this->assertTrue($url->hasPath());
118+
$this->assertTrue($url->hasQueryParameterWithKey('arg1'));
111119

112120
$this->assertEquals(2, $url->countQueryParameters());
113121

0 commit comments

Comments
 (0)