| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* League.Uri (https://uri.thephpleague.com) |
| 5 |
* |
| 6 |
* (c) Ignace Nyamagana Butera <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace League\Uri; |
| 15 |
|
| 16 |
use League\Uri\Contracts\UriInterface; |
| 17 |
use League\Uri\Exceptions\SyntaxError; |
| 18 |
use Psr\Http\Message\UriInterface as Psr7UriInterface; |
| 19 |
use function is_object; |
| 20 |
use function is_scalar; |
| 21 |
use function method_exists; |
| 22 |
use function sprintf; |
| 23 |
|
| 24 |
final class Http implements Psr7UriInterface, \JsonSerializable |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var UriInterface |
| 28 |
*/ |
| 29 |
private $uri; |
| 30 |
|
| 31 |
/** |
| 32 |
* New instance. |
| 33 |
*/ |
| 34 |
private function __construct(UriInterface $uri) |
| 35 |
{ |
| 36 |
$this->validate($uri); |
| 37 |
$this->uri = $uri; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Validate the submitted uri against PSR-7 UriInterface. |
| 42 |
* |
| 43 |
* @throws SyntaxError if the given URI does not follow PSR-7 UriInterface rules |
| 44 |
*/ |
| 45 |
private function validate(UriInterface $uri): void |
| 46 |
{ |
| 47 |
$scheme = $uri->getScheme(); |
| 48 |
if (null === $scheme && '' === $uri->getHost()) { |
| 49 |
throw new SyntaxError(sprintf('an URI without scheme can not contains a empty host string according to PSR-7: %s', (string) $uri)); |
| 50 |
} |
| 51 |
|
| 52 |
$port = $uri->getPort(); |
| 53 |
if (null !== $port && ($port < 0 || $port > 65535)) { |
| 54 |
throw new SyntaxError(sprintf('The URI port is outside the established TCP and UDP port ranges: %s', (string) $uri->getPort())); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Static method called by PHP's var export. |
| 60 |
*/ |
| 61 |
public static function __set_state(array $components): self |
| 62 |
{ |
| 63 |
return new self($components['uri']); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Create a new instance from a string. |
| 68 |
* |
| 69 |
* @param string|mixed $uri |
| 70 |
*/ |
| 71 |
public static function createFromString($uri = ''): self |
| 72 |
{ |
| 73 |
return new self(Uri::createFromString($uri)); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Create a new instance from a hash of parse_url parts. |
| 78 |
* |
| 79 |
* @param array $components a hash representation of the URI similar |
| 80 |
* to PHP parse_url function result |
| 81 |
*/ |
| 82 |
public static function createFromComponents(array $components): self |
| 83 |
{ |
| 84 |
return new self(Uri::createFromComponents($components)); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Create a new instance from the environment. |
| 89 |
*/ |
| 90 |
public static function createFromServer(array $server): self |
| 91 |
{ |
| 92 |
return new self(Uri::createFromServer($server)); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Create a new instance from a URI and a Base URI. |
| 97 |
* |
| 98 |
* The returned URI must be absolute. |
| 99 |
* |
| 100 |
* @param mixed $uri the input URI to create |
| 101 |
* @param mixed $base_uri the base URI used for reference |
| 102 |
*/ |
| 103 |
public static function createFromBaseUri($uri, $base_uri = null): self |
| 104 |
{ |
| 105 |
return new self(Uri::createFromBaseUri($uri, $base_uri)); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Create a new instance from a URI object. |
| 110 |
* |
| 111 |
* @param Psr7UriInterface|UriInterface $uri the input URI to create |
| 112 |
*/ |
| 113 |
public static function createFromUri($uri): self |
| 114 |
{ |
| 115 |
if ($uri instanceof UriInterface) { |
| 116 |
return new self($uri); |
| 117 |
} |
| 118 |
|
| 119 |
return new self(Uri::createFromUri($uri)); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* {@inheritDoc} |
| 124 |
*/ |
| 125 |
public function getScheme(): string |
| 126 |
{ |
| 127 |
return (string) $this->uri->getScheme(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* {@inheritDoc} |
| 132 |
*/ |
| 133 |
public function getAuthority(): string |
| 134 |
{ |
| 135 |
return (string) $this->uri->getAuthority(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* {@inheritDoc} |
| 140 |
*/ |
| 141 |
public function getUserInfo(): string |
| 142 |
{ |
| 143 |
return (string) $this->uri->getUserInfo(); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* {@inheritDoc} |
| 148 |
*/ |
| 149 |
public function getHost(): string |
| 150 |
{ |
| 151 |
return (string) $this->uri->getHost(); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* {@inheritDoc} |
| 156 |
*/ |
| 157 |
public function getPort(): ?int |
| 158 |
{ |
| 159 |
return $this->uri->getPort(); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* {@inheritDoc} |
| 164 |
*/ |
| 165 |
public function getPath(): string |
| 166 |
{ |
| 167 |
return $this->uri->getPath(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* {@inheritDoc} |
| 172 |
*/ |
| 173 |
public function getQuery(): string |
| 174 |
{ |
| 175 |
return (string) $this->uri->getQuery(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* {@inheritDoc} |
| 180 |
*/ |
| 181 |
public function getFragment(): string |
| 182 |
{ |
| 183 |
return (string) $this->uri->getFragment(); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* {@inheritDoc} |
| 188 |
*/ |
| 189 |
public function withScheme($scheme): self |
| 190 |
{ |
| 191 |
$scheme = $this->filterInput($scheme); |
| 192 |
if ('' === $scheme) { |
| 193 |
$scheme = null; |
| 194 |
} |
| 195 |
|
| 196 |
$uri = $this->uri->withScheme($scheme); |
| 197 |
if ($uri->getScheme() === $this->uri->getScheme()) { |
| 198 |
return $this; |
| 199 |
} |
| 200 |
|
| 201 |
return new self($uri); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Safely stringify input when possible. |
| 206 |
* |
| 207 |
* @param mixed $str the value to evaluate as a string |
| 208 |
* |
| 209 |
* @throws SyntaxError if the submitted data can not be converted to string |
| 210 |
* |
| 211 |
* @return string|mixed |
| 212 |
*/ |
| 213 |
private function filterInput($str) |
| 214 |
{ |
| 215 |
if (is_scalar($str) || (is_object($str) && method_exists($str, '__toString'))) { |
| 216 |
return (string) $str; |
| 217 |
} |
| 218 |
|
| 219 |
return $str; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* {@inheritDoc} |
| 224 |
*/ |
| 225 |
public function withUserInfo($user, $password = null): self |
| 226 |
{ |
| 227 |
$user = $this->filterInput($user); |
| 228 |
if ('' === $user) { |
| 229 |
$user = null; |
| 230 |
} |
| 231 |
|
| 232 |
$uri = $this->uri->withUserInfo($user, $password); |
| 233 |
if ($uri->getUserInfo() === $this->uri->getUserInfo()) { |
| 234 |
return $this; |
| 235 |
} |
| 236 |
|
| 237 |
return new self($uri); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* {@inheritDoc} |
| 242 |
*/ |
| 243 |
public function withHost($host): self |
| 244 |
{ |
| 245 |
$host = $this->filterInput($host); |
| 246 |
if ('' === $host) { |
| 247 |
$host = null; |
| 248 |
} |
| 249 |
|
| 250 |
$uri = $this->uri->withHost($host); |
| 251 |
if ($uri->getHost() === $this->uri->getHost()) { |
| 252 |
return $this; |
| 253 |
} |
| 254 |
|
| 255 |
return new self($uri); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* {@inheritDoc} |
| 260 |
*/ |
| 261 |
public function withPort($port): self |
| 262 |
{ |
| 263 |
$uri = $this->uri->withPort($port); |
| 264 |
if ($uri->getPort() === $this->uri->getPort()) { |
| 265 |
return $this; |
| 266 |
} |
| 267 |
|
| 268 |
return new self($uri); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* {@inheritDoc} |
| 273 |
*/ |
| 274 |
public function withPath($path): self |
| 275 |
{ |
| 276 |
$uri = $this->uri->withPath($path); |
| 277 |
if ($uri->getPath() === $this->uri->getPath()) { |
| 278 |
return $this; |
| 279 |
} |
| 280 |
|
| 281 |
return new self($uri); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* {@inheritDoc} |
| 286 |
*/ |
| 287 |
public function withQuery($query): self |
| 288 |
{ |
| 289 |
$query = $this->filterInput($query); |
| 290 |
if ('' === $query) { |
| 291 |
$query = null; |
| 292 |
} |
| 293 |
|
| 294 |
$uri = $this->uri->withQuery($query); |
| 295 |
if ($uri->getQuery() === $this->uri->getQuery()) { |
| 296 |
return $this; |
| 297 |
} |
| 298 |
|
| 299 |
return new self($uri); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* {@inheritDoc} |
| 304 |
*/ |
| 305 |
public function withFragment($fragment): self |
| 306 |
{ |
| 307 |
$fragment = $this->filterInput($fragment); |
| 308 |
if ('' === $fragment) { |
| 309 |
$fragment = null; |
| 310 |
} |
| 311 |
|
| 312 |
$uri = $this->uri->withFragment($fragment); |
| 313 |
if ($uri->getFragment() === $this->uri->getFragment()) { |
| 314 |
return $this; |
| 315 |
} |
| 316 |
|
| 317 |
return new self($uri); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* {@inheritDoc} |
| 322 |
*/ |
| 323 |
public function __toString(): string |
| 324 |
{ |
| 325 |
return $this->uri->__toString(); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* {@inheritDoc} |
| 330 |
*/ |
| 331 |
public function jsonSerialize(): string |
| 332 |
{ |
| 333 |
return $this->uri->__toString(); |
| 334 |
} |
| 335 |
} |
| 336 |
|