|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Polyfill\Php85\Uri; |
| 4 | + |
| 5 | +use Symfony\Polyfill\Php85\Exception\ParsingException; |
| 6 | + |
| 7 | +/** |
| 8 | + * @author Alexandre Daubois <[email protected]> |
| 9 | + * |
| 10 | + * @internal |
| 11 | + */ |
| 12 | +class Rfc3986Uri extends \Uri\Uri |
| 13 | +{ |
| 14 | + public function __construct(string $uri, ?string $baseUrl = null) |
| 15 | + { |
| 16 | + if ('' === trim($uri)) { |
| 17 | + throw new \ValueError('Argument #1 ($uri) cannot be empty'); |
| 18 | + } |
| 19 | + |
| 20 | + if (null !== $baseUrl && '' === trim($baseUrl)) { |
| 21 | + throw new \ValueError('Argument #2 ($baseUrl) cannot be empty'); |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + $this->parse($uri, $baseUrl); |
| 26 | + } catch (ParsingException) { |
| 27 | + throw new \Error('Argument #1 ($uri) must be a valid URI'); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private function parse(string $uri, ?string $baseUrl): void |
| 32 | + { |
| 33 | + if (!preg_match('/^[a-zA-Z][a-zA-Z\d+\-.]*:/', $uri) && null !== $baseUrl) { |
| 34 | + // uri is a relative uri and bse url exists |
| 35 | + $this->parse(rtrim($baseUrl, '/').'/'.ltrim($uri, '/'), null); |
| 36 | + |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (preg_match('/[^\x20-\x7e]/', $uri)) { |
| 41 | + // the string contains non-ascii chars |
| 42 | + throw new ParsingException(); |
| 43 | + } |
| 44 | + |
| 45 | + preg_match(self::URI_GLOBAL_REGEX, $uri, $matches); |
| 46 | + if (!$matches || !isset($matches['scheme']) || '' === $matches['scheme']) { |
| 47 | + //throw new InvalidUriException($uri); |
| 48 | + } |
| 49 | + |
| 50 | + if (preg_match('~'.$matches['scheme'].':/(?!/)~', $uri)) { |
| 51 | + //throw new InvalidUriException($uri); |
| 52 | + } |
| 53 | + |
| 54 | + if (isset($matches['authority'])) { |
| 55 | + if (!str_contains($uri, '://') && '' !== $matches['authority']) { |
| 56 | + //throw new InvalidUriException($uri); |
| 57 | + } |
| 58 | + |
| 59 | + preg_match(self::URI_AUTHORITY_REGEX, $matches['authority'], $authMatches); |
| 60 | + |
| 61 | + $matches = array_merge($matches, $authMatches); |
| 62 | + unset($matches['authority']); |
| 63 | + } |
| 64 | + |
| 65 | + $matches = array_filter($matches, function (string $value) { return '' !== $value; }); |
| 66 | + |
| 67 | + if (isset($matches['host']) && false === \filter_var($matches['host'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { |
| 68 | + // the host contains invalid code points |
| 69 | + throw new ParsingException(); |
| 70 | + } |
| 71 | + |
| 72 | + $this->scheme = $matches['scheme'] ?? null; |
| 73 | + $this->user = isset($matches['user']) ? rawurldecode($matches['user']) : null; |
| 74 | + $this->password = isset($matches['pass']) ? rawurldecode($matches['pass']) : null; |
| 75 | + $this->host = $matches['host'] ?? null; |
| 76 | + $this->port = $matches['port'] ?? null; |
| 77 | + $this->path = isset($matches['path']) ? ltrim($matches['path'], '/') : null; |
| 78 | + $this->query = $matches['query'] ?? null; |
| 79 | + $this->fragment = $matches['fragment'] ?? null; |
| 80 | + } |
| 81 | +} |
0 commit comments