Skip to content

Commit

Permalink
Update the validation engine of comparison-based rules
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 22, 2024
1 parent 692d317 commit 0e87d1f
Show file tree
Hide file tree
Showing 14 changed files with 19 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<string, mixed>
*/
public function getParams(): array
{
return ['compareTo' => $this->compareTo];
return (new Result($this->compare($left, $right), $input, $this))->withParameters($parameters);
}
}
2 changes: 1 addition & 1 deletion library/Rules/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
32 changes: 5 additions & 27 deletions library/Rules/Equivalent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Identical.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Max.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Min.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
11 changes: 0 additions & 11 deletions tests/unit/Rules/EqualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[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<array{Equals, mixed}> */
public static function providerForValidInput(): iterable
{
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Rules/GreaterThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Respect\Validation\Test\Stubs\CountableStub;

#[Group('rule')]
#[CoversClass(AbstractComparison::class)]
#[CoversClass(GreaterThan::class)]
final class GreaterThanTest extends RuleTestCase
{
Expand Down
11 changes: 0 additions & 11 deletions tests/unit/Rules/IdenticalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[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<array{Identical, mixed}> */
public static function providerForValidInput(): iterable
{
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Rules/LessThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Rules/MaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Respect\Validation\Test\Stubs\CountableStub;

#[Group('rule')]
#[CoversClass(AbstractComparison::class)]
#[CoversClass(Max::class)]
final class MaxTest extends RuleTestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Rules/MinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Respect\Validation\Test\Stubs\CountableStub;

#[Group('rule')]
#[CoversClass(AbstractComparison::class)]
#[CoversClass(Min::class)]
final class MinTest extends RuleTestCase
{
Expand Down

0 comments on commit 0e87d1f

Please sign in to comment.