Skip to content

Commit

Permalink
Update the validation engine of age-related 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 25, 2024
1 parent b653b05 commit eb5f9a9
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions library/Rules/AbstractAge.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Helpers\CanValidateDateTime;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;

use function date;
use function date_parse_from_format;
use function is_scalar;
use function strtotime;
use function vsprintf;

abstract class AbstractAge extends AbstractRule
abstract class AbstractAge extends Standard
{
use CanValidateDateTime;

Expand All @@ -32,25 +34,18 @@ public function __construct(
$this->baseDate = (int) date('Ymd') - $this->age * 10000;
}

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

if ($this->format === null) {
return $this->isValidWithoutFormat((string) $input);
return new Result($this->isValidWithoutFormat((string) $input), $input, $this, $parameters);
}

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

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

private function isValidWithoutFormat(string $dateTime): bool
Expand Down

0 comments on commit eb5f9a9

Please sign in to comment.