Skip to content
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

[11.x] Add fluent RegExp validation class rule #54521

Closed
wants to merge 17 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
12 changes: 12 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Validation\Rules\NotIn;
use Illuminate\Validation\Rules\Numeric;
use Illuminate\Validation\Rules\ProhibitedIf;
use Illuminate\Validation\Rules\RegExp;
use Illuminate\Validation\Rules\RequiredIf;
use Illuminate\Validation\Rules\Unique;

Expand Down Expand Up @@ -243,4 +244,15 @@ public static function numeric()
{
return new Numeric;
}

/**
* Get a regex rule builder rule instance.
*
* @param string $expression
* @param string[]|\Illuminate\Contracts\Support\Arrayable $extraFlags
*/
public static function regex(string $expression, array|Arrayable $extraFlags = [])
{
return new RegExp($expression, $extraFlags);
}
}
81 changes: 81 additions & 0 deletions src/Illuminate/Validation/Rules/RegExp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Illuminate\Validation\Rules;

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

class RegExp implements Stringable
{
protected string $expression;
protected bool $negated = false;

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

/**
* Create a new regex rule instance.
*
* @param string $expression
* @param string[]|\Illuminate\Contracts\Support\Arrayable $extraFlags
* @return void
*/
public function __construct(string $expression, array|Arrayable $extraFlags = [])
{
$str = Str::of($expression);
$currentFlags = str_split($str->afterLast('/'));

if ($extraFlags instanceof Arrayable) {
$extraFlags = $extraFlags->toArray();
}

$this->expression = $str->beforeLast('/')->append('/')->toString();
$this->flags = array_unique([...$currentFlags, ...$extraFlags]);
}

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

return $this;
}

public function flag(string $flag)
{
$this->flags = array_unique([...$this->flags, $flag]);

return $this;
}

public function not()
{
$this->negated = true;

return $this;
}

/**
* Convert the rule to a validation string.
*
* @return string
*/
public function __toString()
{
return sprintf(
'%s:%s%s',
$this->negated ? 'not_regex' : 'regex',
$this->expression,
implode('', $this->flags),
);
}
}
77 changes: 77 additions & 0 deletions tests/Validation/ValidationRegExpRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Illuminate\Tests\Validation;

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

class ValidationRegExpRuleTest extends TestCase
{
public function testRegExpRuleStringification()
{
$rule = Rule::regex('/[a-z]/i');

$this->assertSame('regex:/[a-z]/i', (string) $rule);
}

public function testNotRegExpRuleStringification()
{
$rule = Rule::regex('/[a-z]/i')->not();

$this->assertSame('not_regex:/[a-z]/i', (string) $rule);
}

#[TestWith(['/[a-z]/', ['i'], 'regex:/[a-z]/i'])]
#[TestWith(['/[a-z]/i', ['i'], 'regex:/[a-z]/i'])]
#[TestWith(['/[a-z]/g', ['i'], 'regex:/[a-z]/gi'])]
public function testRegExpRuleConstructorFlagsStringification(string $input, array $flags, string $output)
{
$rule = Rule::regex($input, $flags);

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

public function tesRegExpRuleConstructorFlagDataTypesStringification()
{
$rule = Rule::regex('/[a-z]/', []);

$this->assertSame('regex:/[a-z]/', (string) $rule);

$rule = Rule::regex('/[a-z]/', ['i']);

$this->assertSame('regex:/[a-z]/i', (string) $rule);

$rule = Rule::regex('/[a-z]/', collect(['i']));

$this->assertSame('regex:/[a-z]/i', (string) $rule);
}

public function testRegExpRuleFlagsStringification()
{
$rule = Rule::regex('/[a-z]/')->flags();

$this->assertSame('regex:/[a-z]/', (string) $rule);

$rule = Rule::regex('/[a-z]/')->flags([]);

$this->assertSame('regex:/[a-z]/', (string) $rule);

$rule = Rule::regex('/[a-z]/')->flags(['i']);

$this->assertSame('regex:/[a-z]/i', (string) $rule);

$rule = Rule::regex('/[a-z]/')->flags(collect(['i']));

$this->assertSame('regex:/[a-z]/i', (string) $rule);
}

#[TestWith(['i', 'regex:/[a-z]/i'])]
#[TestWith(['g', 'regex:/[a-z]/ig'])]
public function testRegExpRuleFlagStringification(string $input, string $output)
{
$rule = Rule::regex('/[a-z]/i')->flag($input);

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