Skip to content

Commit

Permalink
Merge pull request #48 from SamuelNitsche/fix-47
Browse files Browse the repository at this point in the history
Fix creating urls from invalid strings
  • Loading branch information
freekmurze authored Nov 17, 2021
2 parents 2645083 + 03c35bd commit 0a841a9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Exceptions/InvalidArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public static function invalidScheme(string $url): static
return new static("The scheme `{$url}` isn't valid. It should be either `http` or `https`.");
}

public static function invalidUrl(string $url): static
{
return new static("The string `{$url}` is no valid url.");
}

public static function segmentZeroDoesNotExist(): static
{
return new static("Segment 0 doesn't exist. Segments can be retrieved by using 1-based index or a negative index.");
Expand Down
4 changes: 3 additions & 1 deletion src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static function create(): static

public static function fromString(string $url): static
{
$parts = array_merge(parse_url($url));
if (! $parts = parse_url($url)) {
throw InvalidArgument::invalidUrl($url);
}

$url = new static();
$url->scheme = isset($parts['scheme']) ? $url->sanitizeScheme($parts['scheme']) : '';
Expand Down
8 changes: 8 additions & 0 deletions tests/UrlParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public function it_throws_an_exception_if_an_invalid_scheme_is_provided()
Url::fromString('htps://spatie.be');
}

/** @test */
public function it_throws_an_exception_if_a_totally_invalid_url_is_provided()
{
$this->expectException(InvalidArgument::class);

Url::fromString('///remote/fgt_lang?lang=/../../../..//////////dev/');
}

/** @test */
public function it_can_parse_a_host()
{
Expand Down

0 comments on commit 0a841a9

Please sign in to comment.