Passed
Pull Request — master (#222)
by Alexander
02:25
created

CompareToHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 5
eloc 25
dl 0
loc 61
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

2 Methods

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