From 9f73aa912271ce086e73a8a17868f38ad37b764a Mon Sep 17 00:00:00 2001 From: Yannis Rizos Date: Fri, 17 Jan 2025 18:32:38 +0200 Subject: [PATCH 1/7] Introduce URL type --- src/String/Url.php | 30 ++++++++++++++++++++ tests/String/UrlTest.php | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/String/Url.php create mode 100644 tests/String/UrlTest.php diff --git a/src/String/Url.php b/src/String/Url.php new file mode 100644 index 0000000..7994a2b --- /dev/null +++ b/src/String/Url.php @@ -0,0 +1,30 @@ +value = $value; + } + + final public function getValue(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/tests/String/UrlTest.php b/tests/String/UrlTest.php new file mode 100644 index 0000000..2aa37b2 --- /dev/null +++ b/tests/String/UrlTest.php @@ -0,0 +1,59 @@ +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'); + } +} \ No newline at end of file From f8cb421821f569ed82993eaedeb30a3188587b30 Mon Sep 17 00:00:00 2001 From: Yannis Rizos Date: Fri, 17 Jan 2025 18:35:05 +0200 Subject: [PATCH 2/7] Add newline at end of file --- src/String/Url.php | 9 ++++++--- tests/String/UrlTest.php | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/String/Url.php b/src/String/Url.php index 7994a2b..61ad1e5 100644 --- a/src/String/Url.php +++ b/src/String/Url.php @@ -4,6 +4,9 @@ use Epignosis\Types\AbstractType; use InvalidArgumentException; +use const FILTER_NULL_ON_FAILURE; +use const FILTER_SANITIZE_URL; +use const FILTER_VALIDATE_URL; class Url extends AbstractType { @@ -14,9 +17,9 @@ public function __construct(string $value) $value = trim($value); /** @var string|null $value */ - $value = filter_var($value, \FILTER_SANITIZE_URL, \FILTER_NULL_ON_FAILURE); + $value = filter_var($value, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE); - if (!is_string($value) || !filter_var($value, \FILTER_VALIDATE_URL)) { + if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) { throw new InvalidArgumentException('Url is invalid'); } @@ -27,4 +30,4 @@ final public function getValue(): string { return $this->value; } -} \ No newline at end of file +} diff --git a/tests/String/UrlTest.php b/tests/String/UrlTest.php index 2aa37b2..ef0e850 100644 --- a/tests/String/UrlTest.php +++ b/tests/String/UrlTest.php @@ -56,4 +56,4 @@ public function test_CannotBeCreatedByInvalidUrl(): void new Url('www.example.com'); } -} \ No newline at end of file +} From 48d35aafa2b8c0fa157847c34f3ed2a2d10e320e Mon Sep 17 00:00:00 2001 From: Yannis Rizos Date: Fri, 17 Jan 2025 18:36:48 +0200 Subject: [PATCH 3/7] Separate header blocks --- src/String/Url.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/String/Url.php b/src/String/Url.php index 61ad1e5..a3322b4 100644 --- a/src/String/Url.php +++ b/src/String/Url.php @@ -4,6 +4,7 @@ use Epignosis\Types\AbstractType; use InvalidArgumentException; + use const FILTER_NULL_ON_FAILURE; use const FILTER_SANITIZE_URL; use const FILTER_VALIDATE_URL; From 0c48db6a395837ff408a87bff75d464d8f4a9c12 Mon Sep 17 00:00:00 2001 From: Yannis Rizos Date: Mon, 20 Jan 2025 12:22:33 +0200 Subject: [PATCH 4/7] Add test specifically for trim --- tests/String/UrlTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/String/UrlTest.php b/tests/String/UrlTest.php index ef0e850..2de616c 100644 --- a/tests/String/UrlTest.php +++ b/tests/String/UrlTest.php @@ -15,6 +15,13 @@ public function test_CanBeCreatedByUrl(): void $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/'); From 6e9978ab66b435e15b86538b7e6c0b6b5d9d3183 Mon Sep 17 00:00:00 2001 From: Yannis Rizos Date: Mon, 20 Jan 2025 13:52:22 +0200 Subject: [PATCH 5/7] Remove necessary trim --- src/String/Url.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/String/Url.php b/src/String/Url.php index a3322b4..c8ea805 100644 --- a/src/String/Url.php +++ b/src/String/Url.php @@ -15,8 +15,6 @@ class Url extends AbstractType public function __construct(string $value) { - $value = trim($value); - /** @var string|null $value */ $value = filter_var($value, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE); From 670da5316edf5d2e2f730795a4959a92b466efa8 Mon Sep 17 00:00:00 2001 From: Yannis Rizos Date: Tue, 21 Jan 2025 11:52:57 +0200 Subject: [PATCH 6/7] Update src/String/Url.php Co-authored-by: Konstantinos Chatzinikolakis --- src/String/Url.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/String/Url.php b/src/String/Url.php index c8ea805..526b0ff 100644 --- a/src/String/Url.php +++ b/src/String/Url.php @@ -1,5 +1,7 @@ Date: Tue, 21 Jan 2025 12:00:53 +0200 Subject: [PATCH 7/7] Url should extend NonEmptyString --- src/String/Url.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/String/Url.php b/src/String/Url.php index 526b0ff..ea95889 100644 --- a/src/String/Url.php +++ b/src/String/Url.php @@ -4,17 +4,14 @@ namespace Epignosis\Types\String; -use Epignosis\Types\AbstractType; use InvalidArgumentException; use const FILTER_NULL_ON_FAILURE; use const FILTER_SANITIZE_URL; use const FILTER_VALIDATE_URL; -class Url extends AbstractType +class Url extends NonEmptyString { - private string $value; - public function __construct(string $value) { /** @var string|null $value */ @@ -24,11 +21,6 @@ public function __construct(string $value) throw new InvalidArgumentException('Url is invalid'); } - $this->value = $value; - } - - final public function getValue(): string - { - return $this->value; + parent::__construct($value); } }