Add Uri::__toString

This commit is contained in:
PJ Dietz 2015-04-19 20:59:49 -04:00
parent 7dfa3facc1
commit 0fabbc5cb1
2 changed files with 301 additions and 32 deletions

View File

@ -12,8 +12,6 @@ class Uri implements UriInterface
/** @var string */
private $scheme = "";
/** @var string */
private $authority = "";
/** @var string */
private $user = "";
/** @var string|null */
private $password;
@ -67,7 +65,31 @@ class Uri implements UriInterface
*/
public function getAuthority()
{
return $this->authority;
$authority = "";
$host = $this->getHost();
if ($host !== "") {
// User Info
$userInfo = $this->getUserInfo();
if ($userInfo !== "") {
$authority = $userInfo .= "@";
}
// Host
$authority .= $host;
// Port: Include only if set AND non-standard.
$port = $this->getPort();
if ($port !== null) {
$scheme = $this->getScheme();
if (($scheme === "http" && $port !== 80 ) || ($scheme === "https" && $port !== 443)) {
$authority .= ":" . $port;
}
}
}
return $authority;
}
/**
@ -419,7 +441,34 @@ class Uri implements UriInterface
*/
public function __toString()
{
// TODO: Implement __toString() method.
$string = "";
$authority = $this->getAuthority();
if ($authority !== "") {
$scheme = $this->getScheme();
if ($scheme !== "") {
$string = $scheme . ":";
}
$string .= "//$authority";
}
$path = $this->getPath();
if ($path !== "") {
$string .= $path;
}
$query = $this->getQuery();
if ($query !== "") {
$string .= "?$query";
}
$fragment = $this->getFragment();
if ($fragment !== "") {
$string .= "#$fragment";
}
return $string;
}
/**

View File

@ -73,6 +73,116 @@ class UriTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(true);
}
/**
* @covers WellRESTed\Message\Uri::getAuthority
* @dataProvider authorityProvider
* @param string $expected
* @param array $components
*/
public function testConcatenatesAuthorityFromHostAndUserInfo($expected, $components)
{
$uri = new Uri();
if (isset($components["scheme"])) {
$uri = $uri->withScheme($components["scheme"]);
}
if (isset($components["user"])) {
$user = $components["user"];
$password = null;
if (isset($components["password"])) {
$password = $components["password"];
}
$uri = $uri->withUserInfo($user, $password);
}
if (isset($components["host"])) {
$uri = $uri->withHost($components["host"]);
}
if (isset($components["port"])) {
$uri = $uri->withPort($components["port"]);
}
$this->assertEquals($expected, $uri->getAuthority());
}
public function authorityProvider()
{
return [
[
"localhost",
[
"host" => "localhost"
]
],
[
"user@localhost",
[
"host" => "localhost",
"user" => "user"
]
],
[
"user:password@localhost",
[
"host" => "localhost",
"user" => "user",
"password" => "password"
]
],
[
"localhost",
[
"host" => "localhost",
"password" => "password"
]
],
[
"localhost",
[
"scheme" => "http",
"host" => "localhost",
"port" => 80
]
],
[
"localhost",
[
"scheme" => "https",
"host" => "localhost",
"port" => 443
]
],
[
"localhost:4430",
[
"scheme" => "https",
"host" => "localhost",
"port" => 4430
]
],
[
"localhost:8080",
[
"scheme" => "http",
"host" => "localhost",
"port" => 8080
]
],
[
"user:password@localhost:4430",
[
"scheme" => "https",
"user" => "user",
"password" => "password",
"host" => "localhost",
"port" => 4430
]
],
];
}
// ------------------------------------------------------------------------
// User Info
@ -428,4 +538,114 @@ class UriTest extends \PHPUnit_Framework_TestCase
];
}
// ------------------------------------------------------------------------
// Concatenation
/**
* @covers WellRESTed\Message\Uri::__toString
* @dataProvider componentProvider
* @param string $expected
* @param array $components
*/
public function testConcatenatesComponents($expected, $components)
{
$uri = new Uri();
if (isset($components["scheme"])) {
$uri = $uri->withScheme($components["scheme"]);
}
if (isset($components["user"])) {
$user = $components["user"];
$password = null;
if (isset($components["password"])) {
$password = $components["password"];
}
$uri = $uri->withUserInfo($user, $password);
}
if (isset($components["host"])) {
$uri = $uri->withHost($components["host"]);
}
if (isset($components["port"])) {
$uri = $uri->withPort($components["port"]);
}
if (isset($components["path"])) {
$uri = $uri->withPath($components["path"]);
}
if (isset($components["query"])) {
$uri = $uri->withQuery($components["query"]);
}
if (isset($components["fragment"])) {
$uri = $uri->withFragment($components["fragment"]);
}
$this->assertEquals($expected, (string) $uri);
}
public function componentProvider()
{
return [
[
"http://localhost/path",
[
"scheme" => "http",
"host" => "localhost",
"path" => "/path"
]
],
[
"//localhost/path",
[
"host" => "localhost",
"path" => "/path"
]
],
[
"/path",
[
"path" => "/path"
]
],
[
"/path?cat=molly&dog=bear",
[
"path" => "/path",
"query" => "cat=molly&dog=bear"
]
],
[
"/path?cat=molly&dog=bear#fragment",
[
"path" => "/path",
"query" => "cat=molly&dog=bear",
"fragment" => "fragment"
]
],
[
"https://user:password@localhost:4430/path?cat=molly&dog=bear#fragment",
[
"scheme" => "https",
"user" => "user",
"password" => "password",
"host" => "localhost",
"port" => 4430,
"path" => "/path",
"query" => "cat=molly&dog=bear",
"fragment" => "fragment"
]
],
// Asterisk Form
[
"*",
[
"path" => "*"
]
],
];
}
}