diff --git a/src/Exceptions/InvalidArgument.php b/src/Exceptions/InvalidArgument.php index 40929b1..6a63adb 100644 --- a/src/Exceptions/InvalidArgument.php +++ b/src/Exceptions/InvalidArgument.php @@ -8,7 +8,7 @@ class InvalidArgument extends InvalidArgumentException { public static function invalidScheme(string $url): static { - return new static("The scheme `{$url}` isn't valid. It should be either `http` or `https`."); + return new static("The scheme `{$url}` isn't valid. It should be either `http`, `https`, `mailto` or `tel`."); } public static function invalidUrl(string $url): static diff --git a/src/Url.php b/src/Url.php index 0b8cbd8..d111fc9 100644 --- a/src/Url.php +++ b/src/Url.php @@ -27,7 +27,7 @@ class Url implements UriInterface, Stringable protected string $fragment = ''; - public const VALID_SCHEMES = ['http', 'https', 'mailto']; + public const VALID_SCHEMES = ['http', 'https', 'mailto', 'tel']; public function __construct() { @@ -315,11 +315,11 @@ public function __toString(): string { $url = ''; - if ($this->getScheme() !== '' && $this->getScheme() !== 'mailto') { + if ($this->getScheme() !== '' && ! in_array($this->getScheme(), ['mailto', 'tel'], true)) { $url .= $this->getScheme().'://'; } - if ($this->getScheme() === 'mailto' && $this->getPath() !== '') { + if (in_array($this->getScheme(), ['mailto', 'tel'], true) && $this->getPath() !== '') { $url .= $this->getScheme().':'; } @@ -332,7 +332,7 @@ public function __toString(): string } if ($this->getPath() !== '/') { - $path = $this->getScheme() === 'mailto' + $path = in_array($this->getScheme(), ['mailto', 'tel'], true) ? ltrim($this->getPath(), '/') : $this->getPath(); diff --git a/tests/UrlMatchesTest.php b/tests/UrlMatchesTest.php index 419bb81..ee99a2c 100644 --- a/tests/UrlMatchesTest.php +++ b/tests/UrlMatchesTest.php @@ -38,4 +38,12 @@ public function it_can_check_if_it_contains_a_mailto() $this->assertTrue($url->matches(Url::fromString('mailto:email@domain.tld'))); } + + /** @test */ + public function it_can_check_if_it_contains_a_tel() + { + $url = Url::fromString('tel:+3112345678'); + + $this->assertTrue($url->matches(Url::fromString('tel:+3112345678'))); + } } diff --git a/tests/UrlParseTest.php b/tests/UrlParseTest.php index 5c60f85..2ac2ed7 100644 --- a/tests/UrlParseTest.php +++ b/tests/UrlParseTest.php @@ -40,6 +40,22 @@ public function it_can_parse_a_path_with_mailto() $this->assertEquals('email@domain.tld', $url->getPath()); } + /** @test */ + public function it_can_parse_a_scheme_with_tel() + { + $url = Url::fromString('tel:+3112345678'); + + $this->assertEquals('tel', $url->getScheme()); + } + + /** @test */ + public function it_can_parse_a_path_withtel() + { + $url = Url::fromString('tel:+3112345678'); + + $this->assertEquals('+3112345678', $url->getPath()); + } + /** @test */ public function it_throws_an_exception_if_an_invalid_scheme_is_provided() {