Passed
Pull Request — master (#222)
by Dmitriy
02:26
created

CompareToHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
wmc 13
eloc 34
dl 0
loc 70
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compareValues() 0 28 10
A validate() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\CompareTo;
6
7
use Yiisoft\Validator\Result;
8
use Yiisoft\Validator\Rule\RuleHandlerInterface;
9
use Yiisoft\Validator\ValidationContext;
10
use Yiisoft\Validator\ValidatorInterface;
11
use Yiisoft\Validator\Exception\UnexpectedRuleException;
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
    /**
29
     * Constant for specifying the comparison as string values.
30
     * No conversion will be done before comparison.
31
     *
32
     * @see $type
33
     */
34
    public const TYPE_STRING = 'string';
35
    /**
36
     * Constant for specifying the comparison as numeric values.
37
     * String values will be converted into numbers before comparison.
38
     *
39
     * @see $type
40
     */
41
    public const TYPE_NUMBER = 'number';
42
43 31
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
44
    {
45 31
        if (!$rule instanceof CompareTo) {
46 1
            throw new UnexpectedRuleException(CompareTo::class, $rule);
47
        }
48
49 30
        $result = new Result();
50
51 30
        if (!$this->compareValues($rule->operator, $rule->type, $value, $rule->compareValue)) {
52 15
            $result->addError($rule->getMessage(), ['value' => $rule->compareValue]);
53
        }
54
55 30
        return $result;
56
    }
57
58
    /**
59
     * Compares two values with the specified operator.
60
     *
61
     * @param string $operator the comparison operator
62
     * @param string $type the type of the values being compared
63
     * @param mixed $value the value being compared
64
     * @param mixed $compareValue another value being compared
65
     *
66
     * @return bool whether the comparison using the specified operator is true.
67
     */
68 30
    private function compareValues(string $operator, string $type, $value, $compareValue): bool
69
    {
70 30
        if ($type === self::TYPE_NUMBER) {
71
            $value = (float) $value;
72
            $compareValue = (float)$compareValue;
73
        } else {
74 30
            $value = (string) $value;
75 30
            $compareValue = (string) $compareValue;
76
        }
77 30
        switch ($operator) {
78 30
            case '==':
79 5
                return $value == $compareValue;
80 25
            case '===':
81 4
                return $value === $compareValue;
82 21
            case '!=':
83 5
                return $value != $compareValue;
84 16
            case '!==':
85 4
                return $value !== $compareValue;
86 12
            case '>':
87 3
                return $value > $compareValue;
88 9
            case '>=':
89 3
                return $value >= $compareValue;
90 6
            case '<':
91 3
                return $value < $compareValue;
92 3
            case '<=':
93 3
                return $value <= $compareValue;
94
            default:
95
                return false;
96
        }
97
    }
98
}
99