diff --git a/src/Uri.php b/src/Uri.php index cc3ce45..28e6070 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -30,6 +30,12 @@ class Uri implements UriInterface { + public const SUPPORTED_SCHEMES = [ + '' => null, + 'http' => 80, + 'https' => 443 + ]; + /** * Uri scheme (without "://" suffix) * @@ -134,7 +140,7 @@ public function withScheme($scheme) * @return string * * @throws InvalidArgumentException If the Uri scheme is not a string. - * @throws InvalidArgumentException If Uri scheme is not "", "https", or "http". + * @throws InvalidArgumentException If Uri scheme is not exists in SUPPORTED_SCHEMES */ protected function filterScheme($scheme): string { @@ -142,15 +148,11 @@ protected function filterScheme($scheme): string throw new InvalidArgumentException('Uri scheme must be a string.'); } - static $valid = [ - '' => true, - 'https' => true, - 'http' => true, - ]; - $scheme = str_replace('://', '', strtolower($scheme)); - if (!isset($valid[$scheme])) { - throw new InvalidArgumentException('Uri scheme must be one of: "", "https", "http"'); + if (!key_exists($scheme, self::SUPPORTED_SCHEMES)) { + throw new InvalidArgumentException( + 'Uri scheme must be one of: "' . implode('", "', array_keys(static::SUPPORTED_SCHEMES)) . '"' + ); } return $scheme; @@ -300,7 +302,7 @@ public function withPort($port) */ protected function hasStandardPort(): bool { - return ($this->scheme === 'http' && $this->port === 80) || ($this->scheme === 'https' && $this->port === 443); + return static::SUPPORTED_SCHEMES[$this->scheme] === $this->port; } /** diff --git a/tests/UriTest.php b/tests/UriTest.php index 470708b..12a09e2 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -62,7 +62,7 @@ public function testWithSchemeEmpty() public function testWithSchemeInvalid() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Uri scheme must be one of: "", "https", "http"'); + $this->expectExceptionMessageMatches('/^Uri scheme must be one of:.*$/'); $this->uriFactory()->withScheme('ftp'); }