Skip to content

Commit

Permalink
Merge pull request #190 from Stilch/feature-other-schemes
Browse files Browse the repository at this point in the history
Added support for other schemes
  • Loading branch information
l0gicgate committed Apr 29, 2021
2 parents b917409 + 37da6c8 commit e99324d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@

class Uri implements UriInterface
{
public const SUPPORTED_SCHEMES = [
'' => null,
'http' => 80,
'https' => 443
];

/**
* Uri scheme (without "://" suffix)
*
Expand Down Expand Up @@ -134,23 +140,19 @@ 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
{
if (!is_string($scheme)) {
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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit e99324d

Please sign in to comment.