Passed
Pull Request — master (#364)
by
unknown
03:12
created

CompareHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 28
c 2
b 0
f 0
dl 0
loc 54
ccs 28
cts 28
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 22 5
A compareValues() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\Exception\UnexpectedRuleException;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\RuleHandlerInterface;
10
use Yiisoft\Validator\ValidationContext;
11
12
/**
13
 * Compares the specified value with another value.
14
 *
15
 * The value being compared with {@see Compare::$targetValue} or {@see Compare::$targetAttribute}, which is set
16
 * in the constructor.
17
 *
18
 * It supports different comparison operators, specified
19
 * via the {@see Compare::$operator}.
20
 *
21
 * The default comparison function is based on string values, which means the values
22
 * are compared byte by byte. When comparing numbers, make sure to change {@see Compare::$type} to
23
 * {@see Compare::TYPE_NUMBER} to enable numeric comparison.
24
 */
25
final class CompareHandler implements RuleHandlerInterface
26
{
27 72
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
28
    {
29 72
        if (!$rule instanceof Compare) {
30 1
            throw new UnexpectedRuleException(Compare::class, $rule);
31
        }
32
33 71
        $result = new Result();
34 71
        $targetAttribute = $rule->getTargetAttribute();
35 71
        $targetValue = $rule->getTargetValue();
36
37 71
        if ($targetValue === null && $targetAttribute !== null) {
38 4
            $targetValue = $context->getDataSet()?->getAttributeValue($targetAttribute);
39
        }
40
41 71
        if (!$this->compareValues($rule->getOperator(), $rule->getType(), $value, $targetValue)) {
42 39
            $result->addError(
43 39
                $rule->getMessage(),
44 39
                ['targetValueOrAttribute' => $targetValue ?? $targetAttribute],
45
            );
46
        }
47
48 71
        return $result;
49
    }
50
51
    /**
52
     * Compares two values with the specified operator.
53
     *
54
     * @param string $operator The comparison operator.
55
     * @param string $type The type of the values being compared.
56
     * @param mixed $value The value being compared.
57
     * @param mixed $targetValue Another value being compared.
58
     *
59
     * @return bool Whether the comparison using the specified operator is true.
60
     */
61 71
    private function compareValues(string $operator, string $type, mixed $value, mixed $targetValue): bool
62
    {
63 71
        if ($type === Compare::TYPE_NUMBER) {
64 2
            $value = (float)$value;
65 2
            $targetValue = (float)$targetValue;
66
        } else {
67 69
            $value = (string)$value;
68 69
            $targetValue = (string)$targetValue;
69
        }
70 71
        return match ($operator) {
71 14
            '==' => $value == $targetValue,
72 8
            '===' => $value === $targetValue,
73 8
            '!=' => $value != $targetValue,
74 6
            '!==' => $value !== $targetValue,
75 8
            '>' => $value > $targetValue,
76 8
            '>=' => $value >= $targetValue,
77 8
            '<' => $value < $targetValue,
78 71
            '<=' => $value <= $targetValue,
79
        };
80
    }
81
}
82