|
30 | 30 |
|
31 | 31 | class Uri implements UriInterface
|
32 | 32 | {
|
| 33 | + public const SUPPORTED_SCHEMES = [ |
| 34 | + '' => null, |
| 35 | + 'http' => 80, |
| 36 | + 'https' => 443 |
| 37 | + ]; |
| 38 | + |
33 | 39 | /**
|
34 | 40 | * Uri scheme (without "://" suffix)
|
35 | 41 | *
|
@@ -134,23 +140,19 @@ public function withScheme($scheme)
|
134 | 140 | * @return string
|
135 | 141 | *
|
136 | 142 | * @throws InvalidArgumentException If the Uri scheme is not a string.
|
137 |
| - * @throws InvalidArgumentException If Uri scheme is not "", "https", or "http". |
| 143 | + * @throws InvalidArgumentException If Uri scheme is not exists in SUPPORTED_SCHEMES |
138 | 144 | */
|
139 | 145 | protected function filterScheme($scheme): string
|
140 | 146 | {
|
141 | 147 | if (!is_string($scheme)) {
|
142 | 148 | throw new InvalidArgumentException('Uri scheme must be a string.');
|
143 | 149 | }
|
144 | 150 |
|
145 |
| - static $valid = [ |
146 |
| - '' => true, |
147 |
| - 'https' => true, |
148 |
| - 'http' => true, |
149 |
| - ]; |
150 |
| - |
151 | 151 | $scheme = str_replace('://', '', strtolower($scheme));
|
152 |
| - if (!isset($valid[$scheme])) { |
153 |
| - throw new InvalidArgumentException('Uri scheme must be one of: "", "https", "http"'); |
| 152 | + if (!key_exists($scheme, self::SUPPORTED_SCHEMES)) { |
| 153 | + throw new InvalidArgumentException( |
| 154 | + 'Uri scheme must be one of: "' . implode('", "', array_keys(static::SUPPORTED_SCHEMES)) . '"' |
| 155 | + ); |
154 | 156 | }
|
155 | 157 |
|
156 | 158 | return $scheme;
|
@@ -300,7 +302,7 @@ public function withPort($port)
|
300 | 302 | */
|
301 | 303 | protected function hasStandardPort(): bool
|
302 | 304 | {
|
303 |
| - return ($this->scheme === 'http' && $this->port === 80) || ($this->scheme === 'https' && $this->port === 443); |
| 305 | + return static::SUPPORTED_SCHEMES[$this->scheme] === $this->port; |
304 | 306 | }
|
305 | 307 |
|
306 | 308 | /**
|
|
0 commit comments