From bb09289c2d8b2c6161201a3eb2263f8a3efb590e Mon Sep 17 00:00:00 2001 From: Stilch Date: Mon, 12 Apr 2021 18:32:07 +0500 Subject: [PATCH 1/3] Added support others scheme --- src/Uri.php | 22 ++++++++++++---------- tests/UriTest.php | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Uri.php b/src/Uri.php index cc3ce45..2682fe7 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -30,6 +30,12 @@ class Uri implements UriInterface { + protected const SUPPORTED_SCHEME = [ + '' => 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_SCHEME */ 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_SCHEME)) { + throw new InvalidArgumentException( + 'Uri scheme must be one of: "' . implode('", "', array_keys(static::SUPPORTED_SCHEME)) . '"' + ); } 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_SCHEME[$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'); } From c60f0a35ed915846d13b3666ea9c9dc51cd40a26 Mon Sep 17 00:00:00 2001 From: Stilch Date: Tue, 27 Apr 2021 22:45:17 +0500 Subject: [PATCH 2/3] SUPPORTED_SCHEME -> SUPPORTED_SCHEMES --- src/Uri.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Uri.php b/src/Uri.php index 2682fe7..a091c1e 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -30,7 +30,7 @@ class Uri implements UriInterface { - protected const SUPPORTED_SCHEME = [ + protected const SUPPORTED_SCHEMES = [ '' => null, 'http' => 80, 'https' => 443 @@ -140,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 exists in SUPPORTED_SCHEME + * @throws InvalidArgumentException If Uri scheme is not exists in SUPPORTED_SCHEMES */ protected function filterScheme($scheme): string { @@ -149,9 +149,9 @@ protected function filterScheme($scheme): string } $scheme = str_replace('://', '', strtolower($scheme)); - if (!key_exists($scheme, self::SUPPORTED_SCHEME)) { + if (!key_exists($scheme, self::SUPPORTED_SCHEMES)) { throw new InvalidArgumentException( - 'Uri scheme must be one of: "' . implode('", "', array_keys(static::SUPPORTED_SCHEME)) . '"' + 'Uri scheme must be one of: "' . implode('", "', array_keys(static::SUPPORTED_SCHEMES)) . '"' ); } @@ -302,7 +302,7 @@ public function withPort($port) */ protected function hasStandardPort(): bool { - return static::SUPPORTED_SCHEME[$this->scheme] === $this->port; + return static::SUPPORTED_SCHEMES[$this->scheme] === $this->port; } /** From f4caf8cc3e71136a6d132b500f0f763a5559719a Mon Sep 17 00:00:00 2001 From: Stilch Date: Tue, 27 Apr 2021 22:45:46 +0500 Subject: [PATCH 3/3] Public SUPPORTED_SCHEMES --- src/Uri.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uri.php b/src/Uri.php index a091c1e..28e6070 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -30,7 +30,7 @@ class Uri implements UriInterface { - protected const SUPPORTED_SCHEMES = [ + public const SUPPORTED_SCHEMES = [ '' => null, 'http' => 80, 'https' => 443