Skip to content
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: 20 additions & 2 deletions src/Validator/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ class URL extends Validator

protected bool $allowEmpty;

protected bool $allowFragments;

/**
* @param array $allowedSchemes
* @param bool $allowEmpty
* @param bool $allowFragments
*/
public function __construct(array $allowedSchemes = [], bool $allowEmpty = false)
public function __construct(array $allowedSchemes = [], bool $allowEmpty = false, bool $allowFragments = true)
{
$this->allowedSchemes = $allowedSchemes;
$this->allowEmpty = $allowEmpty;
$this->allowFragments = $allowFragments;
}

/**
Expand All @@ -37,7 +41,17 @@ public function __construct(array $allowedSchemes = [], bool $allowEmpty = false
public function getDescription(): string
{
if (!empty($this->allowedSchemes)) {
return 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')';
$description = 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')';

if (!$this->allowFragments) {
$description .= ' and without a fragment component';
}

return $description;
}

if (!$this->allowFragments) {
return 'Value must be a valid URL without a fragment component';
}
Comment thread
ChiragAgg5k marked this conversation as resolved.

return 'Value must be a valid URL';
Expand Down Expand Up @@ -65,6 +79,10 @@ public function isValid($value): bool
return false;
}

if (!$this->allowFragments && \parse_url($value, PHP_URL_FRAGMENT) !== null) {
return false;
}

return true;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Validator/URLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testIsValid(): void
$this->assertSame(false, $this->url->isValid('htt@s://example.com'));
$this->assertSame(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar'));
$this->assertSame(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E'));
$this->assertSame(true, $this->url->isValid('https://example.com/callback#fragment'));
}

public function testIsValidAllowedSchemes(): void
Expand All @@ -64,4 +65,24 @@ public function testAllowEmpty(): void
$this->assertSame(false, $this->url->isValid(''));
$this->assertSame(false, $this->url->isValid(null));
}

public function testDisallowFragments(): void
{
$urlWithoutFragments = new URL(allowFragments: false);

$this->assertSame('Value must be a valid URL without a fragment component', $urlWithoutFragments->getDescription());
$this->assertSame(true, $urlWithoutFragments->isValid('https://example.com/callback'));
$this->assertSame(false, $urlWithoutFragments->isValid('https://example.com/callback#fragment'));
$this->assertSame(false, $urlWithoutFragments->isValid('https://example.com/callback#'));
}

public function testDisallowFragmentsAllowedSchemes(): void
{
$urlWithoutFragments = new URL(['http', 'https'], allowFragments: false);

$this->assertSame('Value must be a valid URL with following schemes (http, https) and without a fragment component', $urlWithoutFragments->getDescription());
$this->assertSame(true, $urlWithoutFragments->isValid('https://example.com/callback'));
$this->assertSame(false, $urlWithoutFragments->isValid('https://example.com/callback#fragment'));
$this->assertSame(false, $urlWithoutFragments->isValid('gopher://www.example.com'));
}
}
Loading