Skip to content

Commit

Permalink
Update validation enginer of a few rules
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Mar 7, 2024
1 parent 8dab012 commit 7ded68c
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 271 deletions.
23 changes: 10 additions & 13 deletions library/Rules/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;

use function mb_strlen;
use function mb_substr;
Expand All @@ -21,7 +23,7 @@
'{{name}} must be a number in the base {{base|raw}}',
'{{name}} must not be a number in the base {{base|raw}}',
)]
final class Base extends AbstractRule
final class Base extends Standard
{
public function __construct(
private readonly int $base,
Expand All @@ -33,18 +35,13 @@ public function __construct(
}
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$valid = mb_substr($this->chars, 0, $this->base);

return (bool) preg_match('@^[' . $valid . ']+$@', (string) $input);
}

/**
* @return array<string, int>
*/
public function getParams(): array
{
return ['base' => $this->base];
return new Result(
(bool) preg_match('@^[' . mb_substr($this->chars, 0, $this->base) . ']+$@', (string) $input),
$input,
$this,
['base' => $this->base]
);
}
}
66 changes: 7 additions & 59 deletions library/Rules/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

namespace Respect\Validation\Rules;

use Respect\Validation\Exceptions\ValidationException;
use ErrorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use Respect\Validation\Validatable;
use Throwable;

Expand All @@ -23,7 +24,7 @@
'{{input}} must be valid when executed with {{callable}}',
'{{input}} must not be valid when executed with {{callable}}',
)]
final class Call extends AbstractRule
final class Call extends Standard
{
/**
* @var callable
Expand All @@ -39,7 +40,10 @@ public function __construct(

public function evaluate(mixed $input): Result
{
$this->setErrorHandler($input);
set_error_handler(static function (int $severity, string $message, ?string $filename, int $line): void {
throw new ErrorException($message, 0, $severity, $filename, $line);
});

try {
return $this->rule->evaluate(call_user_func($this->callable, $input));
} catch (Throwable) {
Expand All @@ -48,60 +52,4 @@ public function evaluate(mixed $input): Result
return Result::failed($input, $this, ['callable' => $this->callable]);
}
}

public function assert(mixed $input): void
{
$this->setErrorHandler($input);

try {
$this->rule->assert(call_user_func($this->callable, $input));
} catch (ValidationException $exception) {
throw $exception;
} catch (Throwable $throwable) {
throw $this->reportError($input);
} finally {
restore_error_handler();
}
}

public function check(mixed $input): void
{
$this->setErrorHandler($input);

try {
$this->rule->check(call_user_func($this->callable, $input));
} catch (ValidationException $exception) {
throw $exception;
} catch (Throwable $throwable) {
throw $this->reportError($input);
} finally {
restore_error_handler();
}
}

public function validate(mixed $input): bool
{
try {
$this->check($input);
} catch (ValidationException $exception) {
return false;
}

return true;
}

/**
* @return array<string, callable>
*/
public function getParams(): array
{
return ['callable' => $this->callable];
}

private function setErrorHandler(mixed $input): void
{
set_error_handler(function () use ($input): void {
throw $this->reportError($input);
});
}
}
26 changes: 13 additions & 13 deletions library/Rules/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;

use function in_array;
use function is_array;
Expand All @@ -21,33 +23,31 @@
'{{name}} must contain the value {{containsValue}}',
'{{name}} must not contain the value {{containsValue}}',
)]
final class Contains extends AbstractRule
final class Contains extends Standard
{
public function __construct(
private readonly mixed $containsValue,
private readonly bool $identical = false
) {
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['containsValue' => $this->containsValue];
if (is_array($input)) {
return in_array($this->containsValue, $input, $this->identical);
return new Result(in_array($this->containsValue, $input, $this->identical), $input, $this, $parameters);
}

if (!is_scalar($input) || !is_scalar($this->containsValue)) {
return false;
return Result::failed($input, $this, $parameters);
}

return $this->validateString((string) $input, (string) $this->containsValue);
}

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

private function validateString(string $haystack, string $needle): bool
Expand Down
19 changes: 7 additions & 12 deletions library/Rules/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Helpers\CanValidateDateTime;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;

use function date;
use function is_scalar;
Expand All @@ -23,7 +25,7 @@
'{{name}} must be a valid date in the format {{sample}}',
'{{name}} must not be a valid date in the format {{sample}}',
)]
final class Date extends AbstractRule
final class Date extends Standard
{
use CanValidateDateTime;

Expand All @@ -35,20 +37,13 @@ public function __construct(
}
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['sample' => date($this->format, strtotime('2005-12-30'))];
if (!is_scalar($input)) {
return false;
return Result::failed($input, $this, $parameters);
}

return $this->isDateTime($this->format, (string) $input);
}

/**
* @return array<string, mixed>
*/
public function getParams(): array
{
return ['sample' => date($this->format, strtotime('2005-12-30'))];
return new Result($this->isDateTime($this->format, (string) $input), $input, $this, $parameters);
}
}
19 changes: 7 additions & 12 deletions library/Rules/EndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;

use function end;
use function is_array;
Expand All @@ -21,29 +23,22 @@
'{{name}} must end with {{endValue}}',
'{{name}} must not end with {{endValue}}',
)]
final class EndsWith extends AbstractRule
final class EndsWith extends Standard
{
public function __construct(
private readonly mixed $endValue,
private readonly bool $identical = false
) {
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['endValue' => $this->endValue];
if ($this->identical) {
return $this->validateIdentical($input);
return new Result($this->validateIdentical($input), $input, $this, $parameters);
}

return $this->validateEquals($input);
}

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

private function validateEquals(mixed $input): bool
Expand Down
21 changes: 8 additions & 13 deletions library/Rules/Factor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;

use function abs;
use function is_integer;
Expand All @@ -19,39 +21,32 @@
'{{name}} must be a factor of {{dividend|raw}}',
'{{name}} must not be a factor of {{dividend|raw}}',
)]
final class Factor extends AbstractRule
final class Factor extends Standard
{
public function __construct(
private readonly int $dividend
) {
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['dividend' => $this->dividend];
// Every integer is a factor of zero, and zero is the only integer that
// has zero for a factor.
if ($this->dividend === 0) {
return true;
return Result::passed($input, $this, $parameters);
}

// Factors must be integers that are not zero.
if (!is_numeric($input) || (int) $input != $input || $input == 0) {
return false;
return Result::failed($input, $this, $parameters);
}

$input = (int) abs((int) $input);
$dividend = (int) abs($this->dividend);

// The dividend divided by the input must be an integer if input is a
// factor of the dividend.
return is_integer($dividend / $input);
}

/**
* @return array<string, int>
*/
public function getParams(): array
{
return ['dividend' => $this->dividend];
return new Result(is_integer($dividend / $input), $input, $this, $parameters);
}
}
3 changes: 2 additions & 1 deletion library/Rules/Iban.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\Rules\Core\Simple;

use function bcmod;
use function is_string;
Expand All @@ -25,7 +26,7 @@
'{{name}} must be a valid IBAN',
'{{name}} must not be a valid IBAN',
)]
final class Iban extends AbstractRule
final class Iban extends Simple
{
private const COUNTRIES_LENGTHS = [
'AL' => 28,
Expand Down
19 changes: 7 additions & 12 deletions library/Rules/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;

use function in_array;
use function is_array;
Expand All @@ -20,29 +22,22 @@
'{{name}} must be in {{haystack}}',
'{{name}} must not be in {{haystack}}',
)]
final class In extends AbstractRule
final class In extends Standard
{
public function __construct(
private readonly mixed $haystack,
private readonly bool $compareIdentical = false
) {
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['haystack' => $this->haystack];
if ($this->compareIdentical) {
return $this->validateIdentical($input);
return new Result($this->validateIdentical($input), $input, $this, $parameters);
}

return $this->validateEquals($input);
}

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

private function validateEquals(mixed $input): bool
Expand Down
Loading

0 comments on commit 7ded68c

Please sign in to comment.