|
| 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 $exception) { |
| 27 | + throw new \Error('Argument #1 ($uri) must be a valid URI'); |
| 28 | + } |
| 29 | + |
| 30 | + $this->initialized = true; |
| 31 | + } |
| 32 | + |
| 33 | + private function parse(string $uri, ?string $baseUrl): void |
| 34 | + { |
| 35 | + if (!preg_match('/^[a-zA-Z][a-zA-Z\d+\-.]*:/', $uri) && null !== $baseUrl) { |
| 36 | + // uri is a relative uri and bse url exists |
| 37 | + $this->parse(rtrim($baseUrl, '/').'/'.ltrim($uri, '/'), null); |
| 38 | + |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (preg_match('/[^\x20-\x7e]/', $uri)) { |
| 43 | + // the string contains non-ascii chars |
| 44 | + throw new ParsingException(); |
| 45 | + } |
| 46 | + |
| 47 | + preg_match(self::URI_GLOBAL_REGEX, $uri, $matches); |
| 48 | + if (!$matches || !isset($matches['scheme']) || '' === $matches['scheme']) { |
| 49 | + //throw new InvalidUriException($uri); |
| 50 | + } |
| 51 | + |
| 52 | + if (preg_match('~'.$matches['scheme'].':/(?!/)~', $uri)) { |
| 53 | + //throw new InvalidUriException($uri); |
| 54 | + } |
| 55 | + |
| 56 | + if (isset($matches['authority'])) { |
| 57 | + if (!str_contains($uri, '://') && '' !== $matches['authority']) { |
| 58 | + //throw new InvalidUriException($uri); |
| 59 | + } |
| 60 | + |
| 61 | + preg_match(self::URI_AUTHORITY_REGEX, $matches['authority'], $authMatches); |
| 62 | + |
| 63 | + $matches = array_merge($matches, $authMatches); |
| 64 | + unset($matches['authority']); |
| 65 | + } |
| 66 | + |
| 67 | + $matches = array_filter($matches, function (string $value) { return '' !== $value; }); |
| 68 | + |
| 69 | + if (isset($matches['host']) && false === \filter_var($matches['host'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { |
| 70 | + // the host contains invalid code points |
| 71 | + throw new ParsingException(); |
| 72 | + } |
| 73 | + |
| 74 | + $this->scheme = $matches['scheme'] ?? null; |
| 75 | + $this->user = isset($matches['user']) ? rawurldecode($matches['user']) : null; |
| 76 | + $this->password = isset($matches['pass']) ? rawurldecode($matches['pass']) : null; |
| 77 | + $this->host = $matches['host'] ?? null; |
| 78 | + $this->port = $matches['port'] ?? null; |
| 79 | + $this->path = isset($matches['path']) ? ltrim($matches['path'], '/') : null; |
| 80 | + $this->query = $matches['query'] ?? null; |
| 81 | + $this->fragment = $matches['fragment'] ?? null; |
| 82 | + } |
| 83 | + |
| 84 | + public function __toString() |
| 85 | + { |
| 86 | + $uri = ''; |
| 87 | + |
| 88 | + if (null !== $this->scheme) { |
| 89 | + $uri .= $this->scheme.':'; |
| 90 | + } |
| 91 | + |
| 92 | + if (null !== $this->host) { |
| 93 | + $uri .= '//'; |
| 94 | + if (null !== $this->user) { |
| 95 | + $uri .= rawurlencode($this->user); |
| 96 | + if (null !== $this->password) { |
| 97 | + $uri .= ':'.rawurlencode($this->password); |
| 98 | + } |
| 99 | + $uri .= '@'; |
| 100 | + } |
| 101 | + $uri .= $this->host; |
| 102 | + if (null !== $this->port) { |
| 103 | + $uri .= ':'.$this->port; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (null !== $this->path) { |
| 108 | + $uri .= '/'.$this->path; |
| 109 | + } |
| 110 | + |
| 111 | + if (null !== $this->query) { |
| 112 | + $uri .= '?'.$this->query; |
| 113 | + } |
| 114 | + |
| 115 | + if (null !== $this->fragment) { |
| 116 | + $uri .= '#'.$this->fragment; |
| 117 | + } |
| 118 | + |
| 119 | + return $uri; |
| 120 | + } |
| 121 | +} |
0 commit comments