Factory
2 months ago
Interfaces
2 months ago
Cookies.php
2 months ago
Environment.php
2 months ago
Header.php
2 months ago
Headers.php
2 months ago
Message.php
2 months ago
NonBufferedBody.php
2 months ago
Request.php
2 months ago
Response.php
2 months ago
Stream.php
2 months ago
UploadedFile.php
2 months ago
Uri.php
2 months ago
Uri.php
503 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Slim Framework (https://slimframework.com) |
| 5 | * |
| 6 | * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License) |
| 7 | */ |
| 8 | |
| 9 | declare(strict_types=1); |
| 10 | |
| 11 | namespace Slim\Psr7; |
| 12 | |
| 13 | use InvalidArgumentException; |
| 14 | use AmeliaVendor\Psr\Http\Message\UriInterface; |
| 15 | |
| 16 | use function filter_var; |
| 17 | use function is_integer; |
| 18 | use function is_null; |
| 19 | use function is_object; |
| 20 | use function is_string; |
| 21 | use function ltrim; |
| 22 | use function method_exists; |
| 23 | use function preg_replace_callback; |
| 24 | use function rawurlencode; |
| 25 | use function str_replace; |
| 26 | use function strtolower; |
| 27 | |
| 28 | use const FILTER_FLAG_IPV6; |
| 29 | use const FILTER_VALIDATE_IP; |
| 30 | |
| 31 | class Uri implements UriInterface |
| 32 | { |
| 33 | public const SUPPORTED_SCHEMES = [ |
| 34 | '' => null, |
| 35 | 'http' => 80, |
| 36 | 'https' => 443 |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * Uri scheme (without "://" suffix) |
| 41 | */ |
| 42 | protected string $scheme = ''; |
| 43 | |
| 44 | protected string $user = ''; |
| 45 | |
| 46 | protected string $password = ''; |
| 47 | |
| 48 | protected string $host = ''; |
| 49 | |
| 50 | protected ?int $port; |
| 51 | |
| 52 | protected string $path = ''; |
| 53 | |
| 54 | /** |
| 55 | * Uri query string (without "?" prefix) |
| 56 | */ |
| 57 | protected string $query = ''; |
| 58 | |
| 59 | /** |
| 60 | * Uri fragment string (without "#" prefix) |
| 61 | */ |
| 62 | protected string $fragment = ''; |
| 63 | |
| 64 | /** |
| 65 | * @param string $scheme Uri scheme. |
| 66 | * @param string $host Uri host. |
| 67 | * @param int|null $port Uri port number. |
| 68 | * @param string $path Uri path. |
| 69 | * @param string $query Uri query string. |
| 70 | * @param string $fragment Uri fragment. |
| 71 | * @param string $user Uri user. |
| 72 | * @param string $password Uri password. |
| 73 | */ |
| 74 | public function __construct( |
| 75 | string $scheme, |
| 76 | string $host, |
| 77 | ?int $port = null, |
| 78 | string $path = '/', |
| 79 | string $query = '', |
| 80 | string $fragment = '', |
| 81 | string $user = '', |
| 82 | string $password = '' |
| 83 | ) { |
| 84 | $this->scheme = $this->filterScheme($scheme); |
| 85 | $this->host = $this->filterHost($host); |
| 86 | $this->port = $this->filterPort($port); |
| 87 | $this->path = $this->filterPath($path); |
| 88 | $this->query = $this->filterQuery($query); |
| 89 | $this->fragment = $this->filterFragment($fragment); |
| 90 | $this->user = $this->filterUserInfo($user); |
| 91 | $this->password = $this->filterUserInfo($password); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * {@inheritdoc} |
| 96 | */ |
| 97 | public function getScheme(): string |
| 98 | { |
| 99 | return $this->scheme; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * {@inheritdoc} |
| 104 | * @return static |
| 105 | */ |
| 106 | public function withScheme($scheme) |
| 107 | { |
| 108 | $scheme = $this->filterScheme($scheme); |
| 109 | $clone = clone $this; |
| 110 | $clone->scheme = $scheme; |
| 111 | |
| 112 | return $clone; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Filter Uri scheme. |
| 117 | * |
| 118 | * @param mixed $scheme Raw Uri scheme. |
| 119 | * |
| 120 | * @return string |
| 121 | * |
| 122 | * @throws InvalidArgumentException If the Uri scheme is not a string. |
| 123 | * @throws InvalidArgumentException If Uri scheme is not exists in SUPPORTED_SCHEMES |
| 124 | */ |
| 125 | protected function filterScheme($scheme): string |
| 126 | { |
| 127 | if (!is_string($scheme)) { |
| 128 | throw new InvalidArgumentException('Uri scheme must be a string.'); |
| 129 | } |
| 130 | |
| 131 | $scheme = str_replace('://', '', strtolower($scheme)); |
| 132 | if (!key_exists($scheme, static::SUPPORTED_SCHEMES)) { |
| 133 | throw new InvalidArgumentException( |
| 134 | 'Uri scheme must be one of: "' . implode('", "', array_keys(static::SUPPORTED_SCHEMES)) . '"' |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | return $scheme; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * {@inheritdoc} |
| 143 | */ |
| 144 | public function getAuthority(): string |
| 145 | { |
| 146 | $userInfo = $this->getUserInfo(); |
| 147 | $host = $this->getHost(); |
| 148 | $port = $this->getPort(); |
| 149 | |
| 150 | return ($userInfo !== '' ? $userInfo . '@' : '') . $host . ($port !== null ? ':' . $port : ''); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * {@inheritdoc} |
| 155 | */ |
| 156 | public function getUserInfo(): string |
| 157 | { |
| 158 | $info = $this->user; |
| 159 | |
| 160 | if ($this->password !== '') { |
| 161 | $info .= ':' . $this->password; |
| 162 | } |
| 163 | |
| 164 | return $info; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * {@inheritdoc} |
| 169 | * @return static |
| 170 | */ |
| 171 | public function withUserInfo($user, $password = null) |
| 172 | { |
| 173 | $clone = clone $this; |
| 174 | $clone->user = $this->filterUserInfo($user); |
| 175 | |
| 176 | if ($clone->user !== '') { |
| 177 | $clone->password = $this->filterUserInfo($password); |
| 178 | } else { |
| 179 | $clone->password = ''; |
| 180 | } |
| 181 | |
| 182 | return $clone; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Filters the user info string. |
| 187 | * |
| 188 | * Returns the percent-encoded query string. |
| 189 | * |
| 190 | * @param string|null $info The raw uri query string. |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | protected function filterUserInfo(?string $info = null): string |
| 195 | { |
| 196 | if (!is_string($info)) { |
| 197 | return ''; |
| 198 | } |
| 199 | |
| 200 | $match = preg_replace_callback( |
| 201 | '/(?:[^%a-zA-Z0-9_\-\.~!\$&\'\(\)\*\+,;=]+|%(?![A-Fa-f0-9]{2}))/', |
| 202 | function ($match) { |
| 203 | return rawurlencode($match[0]); |
| 204 | }, |
| 205 | $info |
| 206 | ); |
| 207 | |
| 208 | return is_string($match) ? $match : ''; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * {@inheritdoc} |
| 213 | */ |
| 214 | public function getHost(): string |
| 215 | { |
| 216 | return $this->host; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * {@inheritdoc} |
| 221 | * @return static |
| 222 | */ |
| 223 | public function withHost($host) |
| 224 | { |
| 225 | $clone = clone $this; |
| 226 | $clone->host = $this->filterHost($host); |
| 227 | |
| 228 | return $clone; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Filter Uri host. |
| 233 | * |
| 234 | * If the supplied host is an IPv6 address, then it is converted to a reference |
| 235 | * as per RFC 2373. |
| 236 | * |
| 237 | * @param mixed $host The host to filter. |
| 238 | * |
| 239 | * @return string |
| 240 | * |
| 241 | * @throws InvalidArgumentException for invalid host names. |
| 242 | */ |
| 243 | protected function filterHost($host): string |
| 244 | { |
| 245 | if (is_object($host) && method_exists($host, '__toString')) { |
| 246 | $host = (string) $host; |
| 247 | } |
| 248 | |
| 249 | if (!is_string($host)) { |
| 250 | throw new InvalidArgumentException('Uri host must be a string'); |
| 251 | } |
| 252 | |
| 253 | if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
| 254 | $host = '[' . $host . ']'; |
| 255 | } |
| 256 | |
| 257 | return strtolower($host); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * {@inheritdoc} |
| 262 | */ |
| 263 | public function getPort(): ?int |
| 264 | { |
| 265 | return $this->port && !$this->hasStandardPort() ? $this->port : null; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * {@inheritdoc} |
| 270 | * @return static |
| 271 | */ |
| 272 | public function withPort($port) |
| 273 | { |
| 274 | $port = $this->filterPort($port); |
| 275 | $clone = clone $this; |
| 276 | $clone->port = $port; |
| 277 | |
| 278 | return $clone; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Does this Uri use a standard port? |
| 283 | * |
| 284 | * @return bool |
| 285 | */ |
| 286 | protected function hasStandardPort(): bool |
| 287 | { |
| 288 | return static::SUPPORTED_SCHEMES[$this->scheme] === $this->port; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Filter Uri port. |
| 293 | * |
| 294 | * @param int|null $port The Uri port number. |
| 295 | * |
| 296 | * @return int|null |
| 297 | * |
| 298 | * @throws InvalidArgumentException If the port is invalid. |
| 299 | */ |
| 300 | protected function filterPort($port): ?int |
| 301 | { |
| 302 | if (is_null($port) || (is_integer($port) && ($port >= 1 && $port <= 65535))) { |
| 303 | return $port; |
| 304 | } |
| 305 | |
| 306 | throw new InvalidArgumentException('Uri port must be null or an integer between 1 and 65535 (inclusive)'); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * {@inheritdoc} |
| 311 | */ |
| 312 | public function getPath(): string |
| 313 | { |
| 314 | $path = $this->path; |
| 315 | |
| 316 | // If the path starts with a / then remove all leading slashes except one. |
| 317 | if (strpos($path, '/') === 0) { |
| 318 | $path = '/' . ltrim($path, '/'); |
| 319 | } |
| 320 | |
| 321 | return $path; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * {@inheritdoc} |
| 326 | * @return static |
| 327 | */ |
| 328 | public function withPath($path) |
| 329 | { |
| 330 | if (!is_string($path)) { |
| 331 | throw new InvalidArgumentException('Uri path must be a string'); |
| 332 | } |
| 333 | |
| 334 | $clone = clone $this; |
| 335 | $clone->path = $this->filterPath($path); |
| 336 | |
| 337 | return $clone; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Filter Uri path. |
| 342 | * |
| 343 | * This method percent-encodes all reserved characters in the provided path string. |
| 344 | * This method will NOT double-encode characters that are already percent-encoded. |
| 345 | * |
| 346 | * @param string $path The raw uri path. |
| 347 | * |
| 348 | * @return string The RFC 3986 percent-encoded uri path. |
| 349 | * |
| 350 | * @link http://www.faqs.org/rfcs/rfc3986.html |
| 351 | */ |
| 352 | protected function filterPath($path): string |
| 353 | { |
| 354 | $match = preg_replace_callback( |
| 355 | '/(?:[^a-zA-Z0-9_\-\.~:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/', |
| 356 | function ($match) { |
| 357 | return rawurlencode($match[0]); |
| 358 | }, |
| 359 | $path |
| 360 | ); |
| 361 | |
| 362 | return is_string($match) ? $match : ''; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * {@inheritdoc} |
| 367 | */ |
| 368 | public function getQuery(): string |
| 369 | { |
| 370 | return $this->query; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * {@inheritdoc} |
| 375 | * @return static |
| 376 | */ |
| 377 | public function withQuery($query) |
| 378 | { |
| 379 | $query = ltrim($this->filterQuery($query), '?'); |
| 380 | $clone = clone $this; |
| 381 | $clone->query = $query; |
| 382 | |
| 383 | return $clone; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Filters the query string of a URI. |
| 388 | * |
| 389 | * Returns the percent-encoded query string. |
| 390 | * |
| 391 | * @param mixed $query The raw uri query string. |
| 392 | * |
| 393 | * @return string |
| 394 | */ |
| 395 | protected function filterQuery($query): string |
| 396 | { |
| 397 | if (is_object($query) && method_exists($query, '__toString')) { |
| 398 | $query = (string) $query; |
| 399 | } |
| 400 | |
| 401 | if (!is_string($query)) { |
| 402 | throw new InvalidArgumentException('Uri query must be a string.'); |
| 403 | } |
| 404 | |
| 405 | $match = preg_replace_callback( |
| 406 | '/(?:[^a-zA-Z0-9_\-\.~!\$&\'\(\)\*\+,;=%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/', |
| 407 | function ($match) { |
| 408 | return rawurlencode($match[0]); |
| 409 | }, |
| 410 | $query |
| 411 | ); |
| 412 | |
| 413 | return is_string($match) ? $match : ''; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * {@inheritdoc} |
| 418 | */ |
| 419 | public function getFragment(): string |
| 420 | { |
| 421 | return $this->fragment; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * {@inheritdoc} |
| 426 | * @return static |
| 427 | */ |
| 428 | public function withFragment($fragment) |
| 429 | { |
| 430 | $fragment = $this->filterFragment($fragment); |
| 431 | $clone = clone $this; |
| 432 | $clone->fragment = $fragment; |
| 433 | |
| 434 | return $clone; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Filters fragment of a URI. |
| 439 | * |
| 440 | * Returns the percent-encoded fragment. |
| 441 | * |
| 442 | * @param mixed $fragment The raw uri query string. |
| 443 | * |
| 444 | * @return string |
| 445 | */ |
| 446 | protected function filterFragment($fragment): string |
| 447 | { |
| 448 | if (is_object($fragment) && method_exists($fragment, '__toString')) { |
| 449 | $fragment = (string) $fragment; |
| 450 | } |
| 451 | |
| 452 | if (!is_string($fragment)) { |
| 453 | throw new InvalidArgumentException('Uri fragment must be a string.'); |
| 454 | } |
| 455 | |
| 456 | $fragment = ltrim($fragment, '#'); |
| 457 | |
| 458 | $match = preg_replace_callback( |
| 459 | '/(?:[^a-zA-Z0-9_\-\.~!\$&\'\(\)\*\+,;=%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/', |
| 460 | function ($match) { |
| 461 | return rawurlencode($match[0]); |
| 462 | }, |
| 463 | $fragment |
| 464 | ); |
| 465 | |
| 466 | return is_string($match) ? $match : ''; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * {@inheritdoc} |
| 471 | */ |
| 472 | public function __toString(): string |
| 473 | { |
| 474 | $scheme = $this->getScheme(); |
| 475 | $authority = $this->getAuthority(); |
| 476 | $path = $this->path; |
| 477 | $query = $this->getQuery(); |
| 478 | $fragment = $this->getFragment(); |
| 479 | |
| 480 | if ($path !== '') { |
| 481 | if ($path[0] !== '/') { |
| 482 | if ($authority !== '') { |
| 483 | // If the path is rootless and an authority is present, the path MUST be prefixed by "/". |
| 484 | $path = '/' . $path; |
| 485 | } |
| 486 | } elseif (isset($path[1]) && $path[1] === '/') { |
| 487 | if ($authority === '') { |
| 488 | // If the path is starting with more than one "/" and no authority is present, |
| 489 | // the starting slashes MUST be reduced to one. |
| 490 | $path = ltrim($path, '/'); |
| 491 | $path = '/' . $path; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return ($scheme !== '' ? $scheme . ':' : '') |
| 497 | . ($authority !== '' ? '//' . $authority : '') |
| 498 | . $path |
| 499 | . ($query !== '' ? '?' . $query : '') |
| 500 | . ($fragment !== '' ? '#' . $fragment : ''); |
| 501 | } |
| 502 | } |
| 503 |