From 0e87d1fafd0a0f3f4bf3d3188dcc498831a6b610 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Mon, 12 Feb 2024 20:14:42 +0100 Subject: [PATCH] Update the validation engine of comparison-based rules Signed-off-by: Henrique Moody --- ...{AbstractComparison.php => Comparison.php} | 19 ++++------- library/Rules/Equals.php | 2 +- library/Rules/Equivalent.php | 32 +++---------------- library/Rules/GreaterThan.php | 2 +- library/Rules/Identical.php | 2 +- library/Rules/LessThan.php | 2 +- library/Rules/Max.php | 2 +- library/Rules/Min.php | 2 +- tests/unit/Rules/EqualsTest.php | 11 ------- tests/unit/Rules/GreaterThanTest.php | 1 - tests/unit/Rules/IdenticalTest.php | 11 ------- tests/unit/Rules/LessThanTest.php | 2 +- tests/unit/Rules/MaxTest.php | 1 - tests/unit/Rules/MinTest.php | 1 - 14 files changed, 19 insertions(+), 71 deletions(-) rename library/Rules/{AbstractComparison.php => Comparison.php} (64%) diff --git a/library/Rules/AbstractComparison.php b/library/Rules/Comparison.php similarity index 64% rename from library/Rules/AbstractComparison.php rename to library/Rules/Comparison.php index 29e9112d6..dc8dbea20 100644 --- a/library/Rules/AbstractComparison.php +++ b/library/Rules/Comparison.php @@ -10,8 +10,9 @@ namespace Respect\Validation\Rules; use Respect\Validation\Helpers\CanCompareValues; +use Respect\Validation\Result; -abstract class AbstractComparison extends AbstractRule +abstract class Comparison extends Standard { use CanCompareValues; @@ -24,23 +25,17 @@ public function __construct(mixed $maxValue) $this->compareTo = $maxValue; } - public function validate(mixed $input): bool + public function evaluate(mixed $input): Result { $left = $this->toComparable($input); $right = $this->toComparable($this->compareTo); + $parameters = ['compareTo' => $this->compareTo]; + if (!$this->isAbleToCompareValues($left, $right)) { - return false; + return Result::failed($input, $this)->withParameters($parameters); } - return $this->compare($left, $right); - } - - /** - * @return array - */ - public function getParams(): array - { - return ['compareTo' => $this->compareTo]; + return (new Result($this->compare($left, $right), $input, $this))->withParameters($parameters); } } diff --git a/library/Rules/Equals.php b/library/Rules/Equals.php index ce7af57d2..fc6cf82f0 100644 --- a/library/Rules/Equals.php +++ b/library/Rules/Equals.php @@ -15,7 +15,7 @@ '{{name}} must equal {{compareTo}}', '{{name}} must not equal {{compareTo}}', )] -final class Equals extends AbstractComparison +final class Equals extends Comparison { protected function compare(mixed $left, mixed $right): bool { diff --git a/library/Rules/Equivalent.php b/library/Rules/Equivalent.php index b7ed8f10e..bf287af4d 100644 --- a/library/Rules/Equivalent.php +++ b/library/Rules/Equivalent.php @@ -18,36 +18,14 @@ '{{name}} must be equivalent to {{compareTo}}', '{{name}} must not be equivalent to {{compareTo}}', )] -final class Equivalent extends AbstractRule +final class Equivalent extends Comparison { - public function __construct( - private readonly mixed $compareTo - ) { - } - - public function validate(mixed $input): bool - { - if (is_scalar($input)) { - return $this->isStringEquivalent((string) $input); - } - - return $input == $this->compareTo; - } - - /** - * @return array - */ - public function getParams(): array - { - return ['compareTo' => $this->compareTo]; - } - - private function isStringEquivalent(string $input): bool + protected function compare(mixed $left, mixed $right): bool { - if (!is_scalar($this->compareTo)) { - return false; + if (!is_scalar($left)) { + return $left == $right; } - return mb_strtoupper((string) $input) === mb_strtoupper((string) $this->compareTo); + return mb_strtoupper((string) $left) === mb_strtoupper((string) $right); } } diff --git a/library/Rules/GreaterThan.php b/library/Rules/GreaterThan.php index 1528f22e5..4be1c7ff1 100644 --- a/library/Rules/GreaterThan.php +++ b/library/Rules/GreaterThan.php @@ -15,7 +15,7 @@ '{{name}} must be greater than {{compareTo}}', '{{name}} must not be greater than {{compareTo}}', )] -final class GreaterThan extends AbstractComparison +final class GreaterThan extends Comparison { protected function compare(mixed $left, mixed $right): bool { diff --git a/library/Rules/Identical.php b/library/Rules/Identical.php index b8f852b42..78cf8bf0d 100644 --- a/library/Rules/Identical.php +++ b/library/Rules/Identical.php @@ -15,7 +15,7 @@ '{{name}} must be identical as {{compareTo}}', '{{name}} must not be identical as {{compareTo}}', )] -final class Identical extends AbstractComparison +final class Identical extends Comparison { protected function compare(mixed $left, mixed $right): bool { diff --git a/library/Rules/LessThan.php b/library/Rules/LessThan.php index 81adf161b..2a8a15cac 100644 --- a/library/Rules/LessThan.php +++ b/library/Rules/LessThan.php @@ -15,7 +15,7 @@ '{{name}} must be less than {{compareTo}}', '{{name}} must not be less than {{compareTo}}', )] -final class LessThan extends AbstractComparison +final class LessThan extends Comparison { protected function compare(mixed $left, mixed $right): bool { diff --git a/library/Rules/Max.php b/library/Rules/Max.php index 6a90ba34d..976f998a3 100644 --- a/library/Rules/Max.php +++ b/library/Rules/Max.php @@ -15,7 +15,7 @@ '{{name}} must be less than or equal to {{compareTo}}', '{{name}} must not be less than or equal to {{compareTo}}', )] -final class Max extends AbstractComparison +final class Max extends Comparison { protected function compare(mixed $left, mixed $right): bool { diff --git a/library/Rules/Min.php b/library/Rules/Min.php index ecced67b6..780ed1fbc 100644 --- a/library/Rules/Min.php +++ b/library/Rules/Min.php @@ -15,7 +15,7 @@ '{{name}} must be greater than or equal to {{compareTo}}', '{{name}} must not be greater than or equal to {{compareTo}}', )] -final class Min extends AbstractComparison +final class Min extends Comparison { protected function compare(mixed $left, mixed $right): bool { diff --git a/tests/unit/Rules/EqualsTest.php b/tests/unit/Rules/EqualsTest.php index 3545d2e48..a38ffb788 100644 --- a/tests/unit/Rules/EqualsTest.php +++ b/tests/unit/Rules/EqualsTest.php @@ -11,7 +11,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\Attributes\Test; use Respect\Validation\Test\RuleTestCase; use stdClass; @@ -19,16 +18,6 @@ #[CoversClass(Equals::class)] final class EqualsTest extends RuleTestCase { - #[Test] - public function shouldPassCompareToParameterToException(): void - { - $compareTo = new stdClass(); - $equals = new Equals($compareTo); - $exception = $equals->reportError('input'); - - self::assertSame($compareTo, $exception->getParam('compareTo')); - } - /** @return iterable */ public static function providerForValidInput(): iterable { diff --git a/tests/unit/Rules/GreaterThanTest.php b/tests/unit/Rules/GreaterThanTest.php index a272a3ccb..eda130e36 100644 --- a/tests/unit/Rules/GreaterThanTest.php +++ b/tests/unit/Rules/GreaterThanTest.php @@ -15,7 +15,6 @@ use Respect\Validation\Test\Stubs\CountableStub; #[Group('rule')] -#[CoversClass(AbstractComparison::class)] #[CoversClass(GreaterThan::class)] final class GreaterThanTest extends RuleTestCase { diff --git a/tests/unit/Rules/IdenticalTest.php b/tests/unit/Rules/IdenticalTest.php index e76cf4d93..a89c33448 100644 --- a/tests/unit/Rules/IdenticalTest.php +++ b/tests/unit/Rules/IdenticalTest.php @@ -11,7 +11,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\Attributes\Test; use Respect\Validation\Test\RuleTestCase; use stdClass; @@ -19,16 +18,6 @@ #[CoversClass(Identical::class)] final class IdenticalTest extends RuleTestCase { - #[Test] - public function shouldPassCompareToParameterToException(): void - { - $compareTo = new stdClass(); - $rule = new Identical($compareTo); - $exception = $rule->reportError('input'); - - self::assertSame($compareTo, $exception->getParam('compareTo')); - } - /** @return iterable */ public static function providerForValidInput(): iterable { diff --git a/tests/unit/Rules/LessThanTest.php b/tests/unit/Rules/LessThanTest.php index 4c1303668..e102c6657 100644 --- a/tests/unit/Rules/LessThanTest.php +++ b/tests/unit/Rules/LessThanTest.php @@ -15,7 +15,7 @@ use Respect\Validation\Test\Stubs\CountableStub; #[Group('rule')] -#[CoversClass(AbstractComparison::class)] +#[CoversClass(Comparison::class)] #[CoversClass(LessThan::class)] final class LessThanTest extends RuleTestCase { diff --git a/tests/unit/Rules/MaxTest.php b/tests/unit/Rules/MaxTest.php index 2e3fdcaa9..ec9ae8f2c 100644 --- a/tests/unit/Rules/MaxTest.php +++ b/tests/unit/Rules/MaxTest.php @@ -17,7 +17,6 @@ use Respect\Validation\Test\Stubs\CountableStub; #[Group('rule')] -#[CoversClass(AbstractComparison::class)] #[CoversClass(Max::class)] final class MaxTest extends RuleTestCase { diff --git a/tests/unit/Rules/MinTest.php b/tests/unit/Rules/MinTest.php index dee667113..58f9c4cbd 100644 --- a/tests/unit/Rules/MinTest.php +++ b/tests/unit/Rules/MinTest.php @@ -17,7 +17,6 @@ use Respect\Validation\Test\Stubs\CountableStub; #[Group('rule')] -#[CoversClass(AbstractComparison::class)] #[CoversClass(Min::class)] final class MinTest extends RuleTestCase {