Skip to content

Commit

Permalink
feature: Uri::getQueryValue (#157)
Browse files Browse the repository at this point in the history
closes #156
  • Loading branch information
g105b authored Jul 19, 2022
1 parent 0b59ba1 commit ff0567f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ public function getQuery():string {
return $this->query ?? "";
}

public function getQueryValue(string $key):?string {
parse_str($this->getQuery(), $queryVariables);
return $queryVariables[$key] ?? null;
}

/**
* Retrieve the fragment component of the URI.
*
Expand Down
9 changes: 8 additions & 1 deletion test/phpunit/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,11 @@ public function testImmutability() {
$this->assertNotSame($uri, $uri->withQuery('q=abc'));
$this->assertNotSame($uri, $uri->withFragment('test'));
}
}

public function testGetQueryValue() {
$uri = new Uri("example.com/test?name=cody&colour=orange");
self::assertSame("cody", $uri->getQueryValue("name"));
self::assertSame("orange", $uri->getQueryValue("colour"));
self::assertNull($uri->getQueryValue("age"));
}
}

0 comments on commit ff0567f

Please sign in to comment.