Passed
Pull Request — master (#364)
by
unknown
02:58
created

CompareHandler::validate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.0572

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 33
ccs 17
cts 19
cp 0.8947
rs 8.8333
cc 7
nc 8
nop 3
crap 7.0572
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use RuntimeException;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Compares the specified value with another value.
15
 *
16
 * The value being compared with {@see Compare::$targetValue} or {@see Compare::$targetAttribute}, which is set
17
 * in the constructor.
18
 *
19
 * It supports different comparison operators, specified
20
 * via the {@see Compare::$operator}.
21
 *
22
 * The default comparison function is based on string values, which means the values
23
 * are compared byte by byte. When comparing numbers, make sure to change {@see Compare::$type} to
24
 * {@see Compare::TYPE_NUMBER} to enable numeric comparison.
25
 */
26
final class CompareHandler implements RuleHandlerInterface
27
{
28 72
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
29
    {
30 72
        if (!$rule instanceof Compare) {
31 1
            throw new UnexpectedRuleException(Compare::class, $rule);
32
        }
33
34 71
        $result = new Result();
35 71
        $targetAttribute = $rule->getTargetAttribute();
36 71
        $targetValue = $rule->getTargetValue();
37
38 71
        if ($targetValue === null && $targetAttribute !== null) {
39
            /** @var mixed $targetValue */
40 4
            $targetValue = $context->getDataSet()?->getAttributeValue($targetAttribute);
41 4
            if (!is_scalar($targetValue)) {
42
                $message = 'The attribute value returned from a custom data set must have a scalar type.';
43
44
                throw new RuntimeException($message);
45
            }
46
        }
47
48 71
        if (!$this->compareValues($rule->getOperator(), $rule->getType(), $value, $targetValue)) {
49 39
            $parameters = [
50 39
                'attribute' => $context->getAttribute(),
51 39
                'targetValue' => $rule->getTargetValue(),
52 39
                'targetAttribute' => $rule->getTargetAttribute(),
53
                'targetValueOrAttribute' => $targetValue ?? $targetAttribute,
54
            ];
55 39
            is_scalar($value) ? $parameters['value'] = $value : $parameters['valueType'] = get_debug_type($value);
56
57 39
            $result->addError($rule->getMessage(), $parameters);
58
        }
59
60 71
        return $result;
61
    }
62
63
    /**
64
     * Compares two values with the specified operator.
65
     *
66
     * @param string $operator The comparison operator.
67
     * @param string $type The type of the values being compared.
68
     * @param mixed $value The value being compared.
69
     * @param mixed $targetValue Another value being compared.
70
     *
71
     * @return bool Whether the comparison using the specified operator is true.
72
     */
73 71
    private function compareValues(string $operator, string $type, mixed $value, mixed $targetValue): bool
74
    {
75 71
        if ($type === Compare::TYPE_NUMBER) {
76 2
            $value = (float)$value;
77 2
            $targetValue = (float)$targetValue;
78
        } else {
79 69
            $value = (string)$value;
80 69
            $targetValue = (string)$targetValue;
81
        }
82 71
        return match ($operator) {
83 14
            '==' => $value == $targetValue,
84 8
            '===' => $value === $targetValue,
85 8
            '!=' => $value != $targetValue,
86 6
            '!==' => $value !== $targetValue,
87 8
            '>' => $value > $targetValue,
88 8
            '>=' => $value >= $targetValue,
89 8
            '<' => $value < $targetValue,
90 71
            '<=' => $value <= $targetValue,
91
        };
92
    }
93
}
94