use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
+use function array_key_exists;
+use function array_keys;
+use function count;
+use function explode;
+use function get_class;
+use function gettype;
+use function implode;
+use function is_numeric;
+use function is_object;
+use function is_string;
+use function ltrim;
+use function parse_url;
+use function preg_replace;
+use function preg_replace_callback;
+use function rawurlencode;
+use function sprintf;
+use function strpos;
+use function strtolower;
+use function substr;
+
/**
* Implementation of Psr\Http\UriInterface.
*
*/
public function __construct($uri = '')
{
+ if ('' === $uri) {
+ return;
+ }
+
if (! is_string($uri)) {
throw new InvalidArgumentException(sprintf(
'URI passed to constructor must be a string; received "%s"',
- (is_object($uri) ? get_class($uri) : gettype($uri))
+ is_object($uri) ? get_class($uri) : gettype($uri)
));
}
- if (! empty($uri)) {
- $this->parseUri($uri);
- }
+ $this->parseUri($uri);
}
/**
*/
public function getAuthority()
{
- if (empty($this->host)) {
+ if ('' === $this->host) {
return '';
}
$authority = $this->host;
- if (! empty($this->userInfo)) {
+ if ('' !== $this->userInfo) {
$authority = $this->userInfo . '@' . $authority;
}
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
- (is_object($scheme) ? get_class($scheme) : gettype($scheme))
+ is_object($scheme) ? get_class($scheme) : gettype($scheme)
));
}
throw new InvalidArgumentException(sprintf(
'%s expects a string user argument; received %s',
__METHOD__,
- (is_object($user) ? get_class($user) : gettype($user))
+ is_object($user) ? get_class($user) : gettype($user)
));
}
if (null !== $password && ! is_string($password)) {
throw new InvalidArgumentException(sprintf(
- '%s expects a string password argument; received %s',
+ '%s expects a string or null password argument; received %s',
__METHOD__,
- (is_object($password) ? get_class($password) : gettype($password))
+ is_object($password) ? get_class($password) : gettype($password)
));
}
$info = $this->filterUserInfoPart($user);
- if ($password) {
+ if (null !== $password) {
$info .= ':' . $this->filterUserInfoPart($password);
}
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
- (is_object($host) ? get_class($host) : gettype($host))
+ is_object($host) ? get_class($host) : gettype($host)
));
}
*/
public function withPort($port)
{
- if (! is_numeric($port) && $port !== null) {
- throw new InvalidArgumentException(sprintf(
- 'Invalid port "%s" specified; must be an integer, an integer string, or null',
- (is_object($port) ? get_class($port) : gettype($port))
- ));
- }
-
if ($port !== null) {
+ if (! is_numeric($port) || is_float($port)) {
+ throw new InvalidArgumentException(sprintf(
+ 'Invalid port "%s" specified; must be an integer, an integer string, or null',
+ is_object($port) ? get_class($port) : gettype($port)
+ ));
+ }
+
$port = (int) $port;
}
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
- (is_object($fragment) ? get_class($fragment) : gettype($fragment))
+ is_object($fragment) ? get_class($fragment) : gettype($fragment)
));
}
{
$uri = '';
- if (! empty($scheme)) {
+ if ('' !== $scheme) {
$uri .= sprintf('%s:', $scheme);
}
- if (! empty($authority)) {
+ if ('' !== $authority) {
$uri .= '//' . $authority;
}
- if ($path) {
- if (empty($path) || '/' !== substr($path, 0, 1)) {
- $path = '/' . $path;
- }
-
- $uri .= $path;
+ if ('' !== $path && '/' !== substr($path, 0, 1)) {
+ $path = '/' . $path;
}
- if ($query) {
+ $uri .= $path;
+
+
+ if ('' !== $query) {
$uri .= sprintf('?%s', $query);
}
- if ($fragment) {
+ if ('' !== $fragment) {
$uri .= sprintf('#%s', $fragment);
}
*/
private function isNonStandardPort($scheme, $host, $port)
{
- if (! $scheme) {
- if ($host && ! $port) {
- return false;
- }
- return true;
+ if ('' === $scheme) {
+ return '' === $host || null !== $port;
}
- if (! $host || ! $port) {
+ if ('' === $host || null === $port) {
return false;
}
$scheme = strtolower($scheme);
$scheme = preg_replace('#:(//)?$#', '', $scheme);
- if (empty($scheme)) {
+ if ('' === $scheme) {
return '';
}
- if (! array_key_exists($scheme, $this->allowedSchemes)) {
+ if (! isset($this->allowedSchemes[$scheme])) {
throw new InvalidArgumentException(sprintf(
'Unsupported scheme "%s"; must be any empty string or in the set (%s)',
$scheme,
$path
);
- if (empty($path)) {
+ if ('' === $path) {
// No path
return $path;
}
*/
private function filterQuery($query)
{
- if (! empty($query) && strpos($query, '?') === 0) {
+ if ('' !== $query && strpos($query, '?') === 0) {
$query = substr($query, 1);
}
private function splitQueryValue($value)
{
$data = explode('=', $value, 2);
- if (1 === count($data)) {
+ if (! isset($data[1])) {
$data[] = null;
}
return $data;
*/
private function filterFragment($fragment)
{
- if (! empty($fragment) && strpos($fragment, '#') === 0) {
+ if ('' !== $fragment && strpos($fragment, '#') === 0) {
$fragment = '%23' . substr($fragment, 1);
}