Passed
Push — master ( 7fa890...f44bc9 )
by
unknown
02:42
created

CompareHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 1
b 0
f 0
dl 0
loc 60
ccs 30
cts 30
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 28 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
                [
45 39
                    'attribute' => $context->getAttribute(),
46 39
                    'targetValue' => $rule->getTargetValue(),
47 39
                    'targetAttribute' => $rule->getTargetAttribute(),
48
                    'targetValueOrAttribute' => $targetValue ?? $targetAttribute,
49
                    'value' => $value,
50
                ],
51
            );
52
        }
53
54 71
        return $result;
55
    }
56
57
    /**
58
     * Compares two values with the specified operator.
59
     *
60
     * @param string $operator The comparison operator.
61
     * @param string $type The type of the values being compared.
62
     * @param mixed $value The value being compared.
63
     * @param mixed $targetValue Another value being compared.
64
     *
65
     * @return bool Whether the comparison using the specified operator is true.
66
     */
67 71
    private function compareValues(string $operator, string $type, mixed $value, mixed $targetValue): bool
68
    {
69 71
        if ($type === Compare::TYPE_NUMBER) {
70 2
            $value = (float)$value;
71 2
            $targetValue = (float)$targetValue;
72
        } else {
73 69
            $value = (string)$value;
74 69
            $targetValue = (string)$targetValue;
75
        }
76 71
        return match ($operator) {
77 14
            '==' => $value == $targetValue,
78 8
            '===' => $value === $targetValue,
79 8
            '!=' => $value != $targetValue,
80 6
            '!==' => $value !== $targetValue,
81 8
            '>' => $value > $targetValue,
82 8
            '>=' => $value >= $targetValue,
83 8
            '<' => $value < $targetValue,
84 71
            '<=' => $value <= $targetValue,
85
        };
86
    }
87
}
88