Passed
Pull Request — master (#246)
by Rustam
02:18
created

CompareToHandler::compareValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 19
ccs 14
cts 16
cp 0.875
rs 9.7333
cc 2
nc 2
nop 4
crap 2.0078
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\Formatter;
9
use Yiisoft\Validator\FormatterInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Compares the specified value with another value.
15
 *
16
 * The value being compared with a constant {@see CompareTo::$compareValue}, which is set
17
 * in the constructor.
18
 *
19
 * It supports different comparison operators, specified
20
 * via the {@see CompareTo::$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 CompareTo::$type} to
24
 * {@see CompareTo::TYPE_NUMBER} to enable numeric comparison.
25
 */
26
final class CompareToHandler implements RuleHandlerInterface
27
{
28
    private FormatterInterface $formatter;
29
30 37
    public function __construct(?FormatterInterface $formatter = null)
31
    {
32 37
        $this->formatter = $formatter ?? new Formatter();
33
    }
34
35 37
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
36
    {
37 37
        if (!$rule instanceof CompareTo) {
38 1
            throw new UnexpectedRuleException(CompareTo::class, $rule);
39
        }
40
41 36
        $result = new Result();
42 36
        $compareAttribute = $rule->getCompareAttribute() ?? $context?->getAttribute() . '_repeat';
43 36
        $compareValue = $rule->getCompareValue() ?? $context?->getDataSet()?->getAttributeValue($compareAttribute);
44
45 36
        if (!$this->compareValues($rule->getOperator(), $rule->getType(), $value, $compareValue)) {
46 18
            $formattedMessage = $this->formatter->format(
47 18
                $rule->getMessage(),
48
                [
49 18
                    'attribute' => $context?->getAttribute(),
50 18
                    'compareValue' => $rule->getCompareValue(),
51 18
                    'compareAttribute' => $rule->getCompareAttribute(),
52 18
                    'compareValueOrAttribute' => $compareValue ?? $compareAttribute,
53
                    'value' => $value,
54
                ]
55
            );
56 18
            $result->addError($formattedMessage);
57
        }
58
59 36
        return $result;
60
    }
61
62
    /**
63
     * Compares two values with the specified operator.
64
     *
65
     * @param string $operator the comparison operator
66
     * @param string $type the type of the values being compared
67
     * @param mixed $value the value being compared
68
     * @param mixed $compareValue another value being compared
69
     *
70
     * @return bool whether the comparison using the specified operator is true.
71
     */
72 36
    private function compareValues(string $operator, string $type, mixed $value, mixed $compareValue): bool
73
    {
74 36
        if ($type === CompareTo::TYPE_NUMBER) {
75
            $value = (float)$value;
76
            $compareValue = (float)$compareValue;
77
        } else {
78 36
            $value = (string)$value;
79 36
            $compareValue = (string)$compareValue;
80
        }
81 36
        return match ($operator) {
82 8
            '==' => $value == $compareValue,
83 5
            '===' => $value === $compareValue,
84 5
            '!=' => $value != $compareValue,
85 4
            '!==' => $value !== $compareValue,
86 3
            '>' => $value > $compareValue,
87 3
            '>=' => $value >= $compareValue,
88 3
            '<' => $value < $compareValue,
89 5
            '<=' => $value <= $compareValue,
90 36
            default => false,
91
        };
92
    }
93
}
94