Skip to content

Commit

Permalink
Merge pull request #53 from mrk-j/add-support-for-tel-scheme
Browse files Browse the repository at this point in the history
Add support for tel: links
  • Loading branch information
freekmurze authored Mar 28, 2022
2 parents 197e9c7 + d2de8c5 commit 9053167
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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().':';
}

Expand All @@ -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();

Expand Down
8 changes: 8 additions & 0 deletions tests/UrlMatchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public function it_can_check_if_it_contains_a_mailto()

$this->assertTrue($url->matches(Url::fromString('mailto:[email protected]')));
}

/** @test */
public function it_can_check_if_it_contains_a_tel()
{
$url = Url::fromString('tel:+3112345678');

$this->assertTrue($url->matches(Url::fromString('tel:+3112345678')));
}
}
16 changes: 16 additions & 0 deletions tests/UrlParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public function it_can_parse_a_path_with_mailto()
$this->assertEquals('[email protected]', $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()
{
Expand Down

0 comments on commit 9053167

Please sign in to comment.