-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from epignosis/add-url-type
Introduce URL type
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Epignosis\Types\String; | ||
|
||
use InvalidArgumentException; | ||
|
||
use const FILTER_NULL_ON_FAILURE; | ||
use const FILTER_SANITIZE_URL; | ||
use const FILTER_VALIDATE_URL; | ||
|
||
class Url extends NonEmptyString | ||
{ | ||
public function __construct(string $value) | ||
{ | ||
/** @var string|null $value */ | ||
$value = filter_var($value, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE); | ||
|
||
if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) { | ||
throw new InvalidArgumentException('Url is invalid'); | ||
} | ||
|
||
parent::__construct($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Epignosis\Types\Tests\String; | ||
|
||
use Epignosis\Types\String\Url; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class UrlTest extends TestCase | ||
{ | ||
public function test_CanBeCreatedByUrl(): void | ||
{ | ||
$url = new Url('https://www.example.com'); | ||
|
||
$this->assertEquals('https://www.example.com', $url->getValue()); | ||
} | ||
|
||
public function test_UrlIsTrimmed(): void | ||
{ | ||
$url = new Url(' https://www.example.com '); | ||
|
||
$this->assertEquals('https://www.example.com', $url->getValue()); | ||
} | ||
|
||
public function test_CanBeCreatedByUrlWithTrailingSlash(): void | ||
{ | ||
$url = new Url('https://www.example.com/'); | ||
|
||
$this->assertEquals('https://www.example.com/', $url->getValue()); | ||
} | ||
|
||
public function test_CanBeCreatedByUrlWithQuery(): void | ||
{ | ||
$url = new Url('https://www.example.com/?query=1'); | ||
|
||
$this->assertEquals('https://www.example.com/?query=1', $url->getValue()); | ||
} | ||
|
||
public function test_CanBeCreatedByUrlWithFragment(): void | ||
{ | ||
$url = new Url('https://www.example.com/#fragment'); | ||
|
||
$this->assertEquals('https://www.example.com/#fragment', $url->getValue()); | ||
} | ||
|
||
public function test_CanBeCreatedByUrlWithQueryAndFragment(): void | ||
{ | ||
$url = new Url('https://www.example.com/?query=1#fragment'); | ||
|
||
$this->assertEquals('https://www.example.com/?query=1#fragment', $url->getValue()); | ||
} | ||
|
||
public function test_CanBeCreatedByUrlWithPort(): void | ||
{ | ||
$url = new Url('https://www.example.com:8080'); | ||
|
||
$this->assertEquals('https://www.example.com:8080', $url->getValue()); | ||
} | ||
|
||
public function test_CannotBeCreatedByInvalidUrl(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
new Url('www.example.com'); | ||
} | ||
} |