Skip to content

Commit ff0567f

Browse files
author
Greg Bowler
authored
feature: Uri::getQueryValue (#157)
closes #156
1 parent 0b59ba1 commit ff0567f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Uri.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ public function getQuery():string {
324324
return $this->query ?? "";
325325
}
326326

327+
public function getQueryValue(string $key):?string {
328+
parse_str($this->getQuery(), $queryVariables);
329+
return $queryVariables[$key] ?? null;
330+
}
331+
327332
/**
328333
* Retrieve the fragment component of the URI.
329334
*

test/phpunit/UriTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,11 @@ public function testImmutability() {
579579
$this->assertNotSame($uri, $uri->withQuery('q=abc'));
580580
$this->assertNotSame($uri, $uri->withFragment('test'));
581581
}
582-
}
582+
583+
public function testGetQueryValue() {
584+
$uri = new Uri("example.com/test?name=cody&colour=orange");
585+
self::assertSame("cody", $uri->getQueryValue("name"));
586+
self::assertSame("orange", $uri->getQueryValue("colour"));
587+
self::assertNull($uri->getQueryValue("age"));
588+
}
589+
}

0 commit comments

Comments
 (0)