Skip to content

Commit e99324d

Browse files
authored
Merge pull request #190 from Stilch/feature-other-schemes
Added support for other schemes
2 parents b917409 + 37da6c8 commit e99324d

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/Uri.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
class Uri implements UriInterface
3232
{
33+
public const SUPPORTED_SCHEMES = [
34+
'' => null,
35+
'http' => 80,
36+
'https' => 443
37+
];
38+
3339
/**
3440
* Uri scheme (without "://" suffix)
3541
*
@@ -134,23 +140,19 @@ public function withScheme($scheme)
134140
* @return string
135141
*
136142
* @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
138144
*/
139145
protected function filterScheme($scheme): string
140146
{
141147
if (!is_string($scheme)) {
142148
throw new InvalidArgumentException('Uri scheme must be a string.');
143149
}
144150

145-
static $valid = [
146-
'' => true,
147-
'https' => true,
148-
'http' => true,
149-
];
150-
151151
$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+
);
154156
}
155157

156158
return $scheme;
@@ -300,7 +302,7 @@ public function withPort($port)
300302
*/
301303
protected function hasStandardPort(): bool
302304
{
303-
return ($this->scheme === 'http' && $this->port === 80) || ($this->scheme === 'https' && $this->port === 443);
305+
return static::SUPPORTED_SCHEMES[$this->scheme] === $this->port;
304306
}
305307

306308
/**

tests/UriTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testWithSchemeEmpty()
6262
public function testWithSchemeInvalid()
6363
{
6464
$this->expectException(InvalidArgumentException::class);
65-
$this->expectExceptionMessage('Uri scheme must be one of: "", "https", "http"');
65+
$this->expectExceptionMessageMatches('/^Uri scheme must be one of:.*$/');
6666

6767
$this->uriFactory()->withScheme('ftp');
6868
}

0 commit comments

Comments
 (0)