Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for other schemes #190

Merged
merged 5 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be static:: here?

Suggested change
if (!key_exists($scheme, self::SUPPORTED_SCHEMES)) {
if (!key_exists($scheme, static::SUPPORTED_SCHEMES)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I fix it.

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