Skip to content

[11.x] Add fluent URL validation class rule #54519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
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
9 changes: 9 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -243,4 +244,12 @@ public static function numeric()
{
return new Numeric;
}

/**
* @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols
*/
public static function url($protocols = null)
{
return new Url(...func_get_args());
}
}
73 changes: 73 additions & 0 deletions src/Illuminate/Validation/Rules/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Illuminate\Validation\Rules;

use Illuminate\Contracts\Support\Arrayable;
use Stringable;

class Url implements Stringable
{
protected bool $active = false;

/**
* @var string[]
*/
protected array $protocols = [];

/**
* Create a new url rule instance.
*
* @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols
* @return void
*/
public function __construct($protocols = null)
{
$this->protocols(...func_get_args());
}

public function active(bool $active)
{
$this->active = $active;

return $this;
}

/**
* @param string[]|\Illuminate\Contracts\Support\Arrayable|null $protocols
*/
public function protocols($protocols = null)
{
$this->protocols = match (true) {
$protocols instanceof Arrayable => $protocols->toArray(),
! is_array($protocols) => func_get_args(),
default => $protocols,
};

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);
}
}
69 changes: 69 additions & 0 deletions tests/Validation/ValidationUrlRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Illuminate\Tests\Validation;

use Illuminate\Validation\Rule;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class ValidationUrlRuleTest extends TestCase
{
public function testUrlRuleStringification()
{
$rule = Rule::url();

$this->assertSame('url', (string) $rule);
}

#[TestWith([true, 'active_url'])]
#[TestWith([false, 'url'])]
public function testActiveUrlRuleStringification(bool $active, string $output)
{
$rule = Rule::url()->active($active);

$this->assertSame($output, (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);
}
}