Skip to content

Commit

Permalink
Merge branch 'bugfix/uri-ToString-method' into 3.x
Browse files Browse the repository at this point in the history
Closes #2449
  • Loading branch information
akrabat committed Jun 24, 2018
2 parents 8ab4b96 + e6917e1 commit 06e126f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Slim/Http/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function getAuthority()
$host = $this->getHost();
$port = $this->getPort();

return ($userInfo ? $userInfo . '@' : '') . $host . ($port !== null ? ':' . $port : '');
return ($userInfo !== '' ? $userInfo . '@' : '') . $host . ($port !== null ? ':' . $port : '');
}

/**
Expand All @@ -358,7 +358,7 @@ public function getAuthority()
*/
public function getUserInfo()
{
return $this->user . ($this->password ? ':' . $this->password : '');
return $this->user . ($this->password !== '' ? ':' . $this->password : '');
}

/**
Expand All @@ -379,8 +379,8 @@ public function withUserInfo($user, $password = null)
{
$clone = clone $this;
$clone->user = $this->filterUserInfo($user);
if ($clone->user) {
$clone->password = $password ? $this->filterUserInfo($password) : '';
if ('' !== $clone->user) {
$clone->password = !in_array($password, [null, ''], true) ? $this->filterUserInfo($password) : '';
} else {
$clone->password = '';
}
Expand Down Expand Up @@ -812,11 +812,11 @@ public function __toString()

$path = $basePath . '/' . ltrim($path, '/');

return ($scheme ? $scheme . ':' : '')
. ($authority ? '//' . $authority : '')
return ($scheme !== '' ? $scheme . ':' : '')
. ($authority !== '' ? '//' . $authority : '')
. $path
. ($query ? '?' . $query : '')
. ($fragment ? '#' . $fragment : '');
. ($query !== '' ? '?' . $query : '')
. ($fragment !== '' ? '#' . $fragment : '');
}

/**
Expand All @@ -834,11 +834,11 @@ public function getBaseUrl()
$authority = $this->getAuthority();
$basePath = $this->getBasePath();

if ($authority && substr($basePath, 0, 1) !== '/') {
if ($authority !== '' && substr($basePath, 0, 1) !== '/') {
$basePath = $basePath . '/' . $basePath;
}

return ($scheme ? $scheme . ':' : '')
return ($scheme !== '' ? $scheme . ':' : '')
. ($authority ? '//' . $authority : '')
. rtrim($basePath, '/');
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Http/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,16 @@ public function testRequestURICanContainParams()
);
$this->assertEquals('abc=123', $uri->getQuery());
}

public function testUriDistinguishZeroFromEmptyString()
{
$expected = 'https://0:0@0:1/0?0#0';
$this->assertSame($expected, (string) Uri::createFromString($expected));
}

public function testGetBaseUrlDistinguishZeroFromEmptyString()
{
$expected = 'https://0:0@0:1/0?0#0';
$this->assertSame('https://0:0@0:1', (string) Uri::createFromString($expected)->getBaseUrl());
}
}

0 comments on commit 06e126f

Please sign in to comment.