Update the validation engine of the "Decimal" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-02-23 01:07:42 +01:00
parent c04034c2a4
commit e520b2d939
No known key found for this signature in database
GPG key ID: 221E9281655813A6

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use function is_numeric;
use function is_string;
@ -21,28 +22,26 @@ use function var_export;
'{{name}} must have {{decimals}} decimals',
'{{name}} must not have {{decimals}} decimals',
)]
final class Decimal extends AbstractRule
final class Decimal extends Standard
{
public function __construct(
private readonly int $decimals
) {
}
public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['decimals' => $this->decimals];
if (!is_numeric($input)) {
return false;
return Result::failed($input, $this)->withParameters($parameters);
}
return $this->toFormattedString($input) === $this->toRawString($input);
return new Result($this->isValidDecimal($input), $input, $this, parameters: $parameters);
}
/**
* @return array<string, int>
*/
public function getParams(): array
private function isValidDecimal(mixed $input): bool
{
return ['decimals' => $this->decimals];
return $this->toFormattedString($input) === $this->toRawString($input);
}
private function toRawString(mixed $input): string