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

CompareToValidator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

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

2 Methods

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