Skip to content

Commit

Permalink
Update the validation engine of the "Regex" rule
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 23, 2024
1 parent 11080c9 commit 5b81e96
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions library/Rules/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Message\Template;
use Respect\Validation\Result;

use function is_scalar;
use function preg_match;
Expand All @@ -18,27 +19,20 @@
'{{name}} must validate against `{{regex|raw}}`',
'{{name}} must not validate against `{{regex|raw}}`',
)]
final class Regex extends AbstractRule
final class Regex extends Standard
{
public function __construct(
private readonly string $regex
) {
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['regex' => $this->regex];
if (!is_scalar($input)) {
return false;
return Result::failed($input, $this)->withParameters($parameters);
}

return preg_match($this->regex, (string) $input) > 0;
}

/**
* @return array<string, string>
*/
public function getParams(): array
{
return ['regex' => $this->regex];
return new Result(preg_match($this->regex, (string) $input) === 1, $input, $this, parameters: $parameters);
}
}

0 comments on commit 5b81e96

Please sign in to comment.