From eabfdc9147e993b6127907dcc4a04eec402af15f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:17:48 +0100 Subject: [PATCH 01/12] Add URL validation class rule --- src/Illuminate/Validation/Rule.php | 6 ++ src/Illuminate/Validation/Rules/Url.php | 75 ++++++++++++++++++++++ tests/Validation/ValidationUrlRuleTest.php | 67 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 src/Illuminate/Validation/Rules/Url.php create mode 100644 tests/Validation/ValidationUrlRuleTest.php diff --git a/src/Illuminate/Validation/Rule.php b/src/Illuminate/Validation/Rule.php index c15772bba7d5..50a5711e68d0 100644 --- a/src/Illuminate/Validation/Rule.php +++ b/src/Illuminate/Validation/Rule.php @@ -20,6 +20,7 @@ use Illuminate\Validation\Rules\ProhibitedIf; use Illuminate\Validation\Rules\RequiredIf; use Illuminate\Validation\Rules\Unique; +use Illuminate\Validation\Rules\Url; class Rule { @@ -243,4 +244,9 @@ public static function numeric() { return new Numeric; } + + public static function url($protocols = null) + { + return new Url($protocols); + } } diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php new file mode 100644 index 000000000000..7e309dd20cc1 --- /dev/null +++ b/src/Illuminate/Validation/Rules/Url.php @@ -0,0 +1,75 @@ +protocols($protocols); + } + + public function active(bool $active) + { + $this->active = $active; + + return $this; + } + + /** + * @param string[]|\Illuminate\Contracts\Support\Arrayable + */ + public function protocols($protocols = null) + { + $this->protocols = match (true) { + $keys instanceof Arrayable => $keys->toArray(), + !is_array($keys) => func_get_args(), + default => $keys, + }; + + return $this; + } + + public function protocol(string $protocol) + { + $this->protocols = array_unique([ ...$this->protocols, $protocol ]); + + return $this; + } + + /** + * Convert the rule to a validation string. + * + * @return string + */ + public function __toString() + { + if ($this->active) { + return 'active_url'; + } + + if ($this->protocols === []) { + return 'url'; + } + + return 'url:'.implode(',', $this->protocols); + } +} diff --git a/tests/Validation/ValidationUrlRuleTest.php b/tests/Validation/ValidationUrlRuleTest.php new file mode 100644 index 000000000000..f2fe44a45447 --- /dev/null +++ b/tests/Validation/ValidationUrlRuleTest.php @@ -0,0 +1,67 @@ +assertSame('url', (string) $rule); + } + + #[TestWith([true, 'active_url'])] + #[TestWith([false, 'url'])] + public function testActiveUrlRuleStringification(bool $active, string $rule) + { + $rule = Rule::url()->active($active); + + $this->assertSame($rule, (string) $rule); + } + + public function testUrlRuleConstructorProtocolsStringification() + { + $rule = Rule::url('http', 'https'); + + $this->assertSame('url:http,https', (string) $rule); + + $rule = Rule::url(['http', 'https']); + + $this->assertSame('url:http,https', (string) $rule); + + $rule = Rule::url(collect(['http', 'https'])); + + $this->assertSame('url:http,https', (string) $rule); + } + + public function testUrlRuleProtocolsStringification() + { + $rule = Rule::url()->protocols('http', 'https'); + + $this->assertSame('url:http,https', (string) $rule); + + $rule = Rule::url()->protocols(['http', 'https']); + + $this->assertSame('url:http,https', (string) $rule); + + $rule = Rule::url()->protocols(collect(['http', 'https'])); + + $this->assertSame('url:http,https', (string) $rule); + + $rule = Rule::url('ftp')->protocols(collect(['http', 'https'])); + + $this->assertSame('url:http,https', (string) $rule); + } + + #[TestWith(['http', 'url:http,https'])] + #[TestWith(['ftp', 'url:http,https,ftp'])] + public function testUrlRuleProtocolStringification(string $input, string $output) + { + $rule = Rule::url('http', 'https')->protocol($input); + + $this->assertSame($output, (string) $rule); + } +} From c1c538ae3fe0bf8595d4d71bc5763c3f2152e103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:28:35 +0100 Subject: [PATCH 02/12] StyleCI --- src/Illuminate/Validation/Rules/Url.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php index 7e309dd20cc1..f14d9d2e069e 100644 --- a/src/Illuminate/Validation/Rules/Url.php +++ b/src/Illuminate/Validation/Rules/Url.php @@ -5,14 +5,12 @@ use Illuminate\Contracts\Support\Arrayable; use Stringable; -use function Illuminate\Support\enum_value; - class Url implements Stringable { protected bool $active = false; - + /** - * @var string[] $protocols + * @var string[] */ protected array $protocols = []; @@ -41,7 +39,7 @@ public function protocols($protocols = null) { $this->protocols = match (true) { $keys instanceof Arrayable => $keys->toArray(), - !is_array($keys) => func_get_args(), + ! is_array($keys) => func_get_args(), default => $keys, }; @@ -50,7 +48,7 @@ public function protocols($protocols = null) public function protocol(string $protocol) { - $this->protocols = array_unique([ ...$this->protocols, $protocol ]); + $this->protocols = array_unique([...$this->protocols, $protocol]); return $this; } From 0c934b369abf0fae92b3d5cdddd17a28504e4abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:29:46 +0100 Subject: [PATCH 03/12] Add missing import --- tests/Validation/ValidationUrlRuleTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Validation/ValidationUrlRuleTest.php b/tests/Validation/ValidationUrlRuleTest.php index f2fe44a45447..fa1a3b56e8cc 100644 --- a/tests/Validation/ValidationUrlRuleTest.php +++ b/tests/Validation/ValidationUrlRuleTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Validation; +use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\TestWith; class ValidationUrlRuleTest extends TestCase From f964fadfd7ccfc385d6767bc0df3753cb808133f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:30:24 +0100 Subject: [PATCH 04/12] StyleCI --- src/Illuminate/Validation/Rules/Url.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php index f14d9d2e069e..5d64f6bab317 100644 --- a/src/Illuminate/Validation/Rules/Url.php +++ b/src/Illuminate/Validation/Rules/Url.php @@ -10,7 +10,7 @@ class Url implements Stringable protected bool $active = false; /** - * @var string[] + * @var string[] */ protected array $protocols = []; From 5f16bfa2297fd797f83c1f7cd42e113f3dd361f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:31:14 +0100 Subject: [PATCH 05/12] Re-order imports --- tests/Validation/ValidationUrlRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Validation/ValidationUrlRuleTest.php b/tests/Validation/ValidationUrlRuleTest.php index fa1a3b56e8cc..ea79cc4a061e 100644 --- a/tests/Validation/ValidationUrlRuleTest.php +++ b/tests/Validation/ValidationUrlRuleTest.php @@ -2,8 +2,8 @@ namespace Illuminate\Tests\Validation; -use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\TestWith; +use PHPUnit\Framework\TestCase; class ValidationUrlRuleTest extends TestCase { From a04f0367429554d5dee0daec97d3ad4c61e410a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:33:19 +0100 Subject: [PATCH 06/12] Fix variable name --- src/Illuminate/Validation/Rules/Url.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php index 5d64f6bab317..4cfdf4335394 100644 --- a/src/Illuminate/Validation/Rules/Url.php +++ b/src/Illuminate/Validation/Rules/Url.php @@ -38,9 +38,9 @@ public function active(bool $active) public function protocols($protocols = null) { $this->protocols = match (true) { - $keys instanceof Arrayable => $keys->toArray(), - ! is_array($keys) => func_get_args(), - default => $keys, + $protocols instanceof Arrayable => $protocols->toArray(), + ! is_array($protocols) => func_get_args(), + default => $protocols, }; return $this; From e7b7c5f4c1585bee6e8c849c254664aef7e5a60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:37:15 +0100 Subject: [PATCH 07/12] Add missing import --- tests/Validation/ValidationUrlRuleTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Validation/ValidationUrlRuleTest.php b/tests/Validation/ValidationUrlRuleTest.php index ea79cc4a061e..8342c87a99ad 100644 --- a/tests/Validation/ValidationUrlRuleTest.php +++ b/tests/Validation/ValidationUrlRuleTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Validation; +use Illuminate\Validation\Rule; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; From 7e72168b8b6a0ee2e9ac6c59e2acb7f42836b1a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:43:48 +0100 Subject: [PATCH 08/12] Fix pass-through arguments --- src/Illuminate/Validation/Rule.php | 5 ++++- src/Illuminate/Validation/Rules/Url.php | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Rule.php b/src/Illuminate/Validation/Rule.php index 50a5711e68d0..da712b56aa2f 100644 --- a/src/Illuminate/Validation/Rule.php +++ b/src/Illuminate/Validation/Rule.php @@ -245,8 +245,11 @@ public static function numeric() return new Numeric; } + /** + * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols + */ public static function url($protocols = null) { - return new Url($protocols); + return new Url(...func_get_args()); } } diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php index 4cfdf4335394..796d9124bb2e 100644 --- a/src/Illuminate/Validation/Rules/Url.php +++ b/src/Illuminate/Validation/Rules/Url.php @@ -17,7 +17,7 @@ class Url implements Stringable /** * Create a new array rule instance. * - * @param array|null $protocols + * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols * @return void */ public function __construct($protocols = null) @@ -33,7 +33,7 @@ public function active(bool $active) } /** - * @param string[]|\Illuminate\Contracts\Support\Arrayable + * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols */ public function protocols($protocols = null) { From 801d9fe614272b695c8c9cd62374176bbdc1c8b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:45:16 +0100 Subject: [PATCH 09/12] Fix duplicate variable names --- tests/Validation/ValidationUrlRuleTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Validation/ValidationUrlRuleTest.php b/tests/Validation/ValidationUrlRuleTest.php index 8342c87a99ad..77585a4256ae 100644 --- a/tests/Validation/ValidationUrlRuleTest.php +++ b/tests/Validation/ValidationUrlRuleTest.php @@ -17,11 +17,11 @@ public function testUrlRuleStringification() #[TestWith([true, 'active_url'])] #[TestWith([false, 'url'])] - public function testActiveUrlRuleStringification(bool $active, string $rule) + public function testActiveUrlRuleStringification(bool $active, string $output) { $rule = Rule::url()->active($active); - $this->assertSame($rule, (string) $rule); + $this->assertSame($output, (string) $rule); } public function testUrlRuleConstructorProtocolsStringification() From dc1666695d577f27058229736b11da85ded1532b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:45:44 +0100 Subject: [PATCH 10/12] StyleCI --- src/Illuminate/Validation/Rule.php | 2 +- src/Illuminate/Validation/Rules/Url.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Rule.php b/src/Illuminate/Validation/Rule.php index da712b56aa2f..37ead70af8a3 100644 --- a/src/Illuminate/Validation/Rule.php +++ b/src/Illuminate/Validation/Rule.php @@ -246,7 +246,7 @@ public static function numeric() } /** - * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols + * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols */ public static function url($protocols = null) { diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php index 796d9124bb2e..eccdbce23b9f 100644 --- a/src/Illuminate/Validation/Rules/Url.php +++ b/src/Illuminate/Validation/Rules/Url.php @@ -33,7 +33,7 @@ public function active(bool $active) } /** - * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols + * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols */ public function protocols($protocols = null) { From e0eacd0cf628a76a333898644291a6fe579d6d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:49:46 +0100 Subject: [PATCH 11/12] Fix pass-through arguments --- src/Illuminate/Validation/Rules/Url.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php index eccdbce23b9f..1f2d43c450b4 100644 --- a/src/Illuminate/Validation/Rules/Url.php +++ b/src/Illuminate/Validation/Rules/Url.php @@ -22,7 +22,7 @@ class Url implements Stringable */ public function __construct($protocols = null) { - $this->protocols($protocols); + $this->protocols(...func_get_args()); } public function active(bool $active) From a20d9d2770842e91dc340e110f9e0ec802c7dc6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Sat, 8 Feb 2025 16:54:33 +0100 Subject: [PATCH 12/12] Fix typo --- src/Illuminate/Validation/Rules/Url.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Rules/Url.php b/src/Illuminate/Validation/Rules/Url.php index 1f2d43c450b4..d251cdb57724 100644 --- a/src/Illuminate/Validation/Rules/Url.php +++ b/src/Illuminate/Validation/Rules/Url.php @@ -15,7 +15,7 @@ class Url implements Stringable protected array $protocols = []; /** - * Create a new array rule instance. + * Create a new url rule instance. * * @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols * @return void