Passed
Pull Request — master (#248)
by Rustam
02:27
created

Compare   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 50
c 1
b 0
f 0
dl 0
loc 140
ccs 30
cts 31
cp 0.9677
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A getOperator() 0 3 1
A getMessage() 0 10 1
A getTargetAttribute() 0 3 1
A __construct() 0 46 2
A getTargetValue() 0 3 1
A getOptions() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Closure;
8
use InvalidArgumentException;
9
use JetBrains\PhpStorm\ArrayShape;
10
use RuntimeException;
11
use Yiisoft\Validator\BeforeValidationInterface;
12
use Yiisoft\Validator\ParametrizedRuleInterface;
13
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
14
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\ValidationContext;
17
18
abstract class Compare implements ParametrizedRuleInterface, BeforeValidationInterface
19
{
20
    use BeforeValidationTrait;
21
    use HandlerClassNameTrait;
22
    use RuleNameTrait;
23
24
    /**
25
     * Constant for specifying the comparison as string values.
26
     * No conversion will be done before comparison.
27
     *
28
     * @see $type
29
     */
30
    public const TYPE_STRING = 'string';
31
    /**
32
     * Constant for specifying the comparison as numeric values.
33
     * String values will be converted into numbers before comparison.
34
     *
35
     * @see $type
36
     */
37
    public const TYPE_NUMBER = 'number';
38
39
    private array $validOperators = [
40
        '==' => 1,
41
        '===' => 1,
42
        '!=' => 1,
43
        '!==' => 1,
44
        '>' => 1,
45
        '>=' => 1,
46
        '<' => 1,
47
        '<=' => 1,
48
    ];
49
50 15
    public function __construct(
51
        /**
52
         * @var mixed The constant value to be compared with. When both this property
53
         * and {@see $targetAttribute} are set, this property takes precedence.
54
         */
55
        private $targetValue = null,
56
        /**
57
         * @var string|null The name of the attribute to be compared with. When both this property
58
         * and {@see $targetValue} are set, the {@see $targetValue} takes precedence.
59
         *
60
         * @see $targetValue
61
         */
62
        private ?string $targetAttribute = null,
63
        /**
64
         * @var string|null User-defined error message.
65
         */
66
        private ?string $message = null,
67
        /**
68
         * @var string The type of the values being compared.
69
         */
70
        private string $type = self::TYPE_STRING,
71
        /**
72
         * @var string The operator for comparison. The following operators are supported:
73
         *
74
         * - `==`: check if two values are equal. The comparison is done is non-strict mode.
75
         * - `===`: check if two values are equal. The comparison is done is strict mode.
76
         * - `!=`: check if two values are NOT equal. The comparison is done is non-strict mode.
77
         * - `!==`: check if two values are NOT equal. The comparison is done is strict mode.
78
         * - `>`: check if value being validated is greater than the value being compared with.
79
         * - `>=`: check if value being validated is greater than or equal to the value being compared with.
80
         * - `<`: check if value being validated is less than the value being compared with.
81
         * - `<=`: check if value being validated is less than or equal to the value being compared with.
82
         *
83
         * When you want to compare numbers, make sure to also change @see $type} to
84
         * {@see TYPE_NUMBER}.
85
         */
86
        private string $operator = '==',
87
        private bool $skipOnEmpty = false,
88
        private bool $skipOnError = false,
89
        /**
90
         * @var Closure(mixed, ValidationContext):bool|null
91
         */
92
        private ?Closure $when = null,
93
    ) {
94 15
        if (!isset($this->validOperators[$operator])) {
95
            throw new InvalidArgumentException("Operator \"$operator\" is not supported.");
96
        }
97
    }
98
99 65
    public function getTargetValue(): mixed
100
    {
101 65
        return $this->targetValue;
102
    }
103
104 65
    public function getTargetAttribute(): ?string
105
    {
106 65
        return $this->targetAttribute;
107
    }
108
109 65
    public function getType(): string
110
    {
111 65
        return $this->type;
112
    }
113
114 65
    public function getOperator(): string
115
    {
116 65
        return $this->operator;
117
    }
118
119 80
    public function getMessage(): string
120
    {
121 80
        return $this->message ?? match ($this->operator) {
122 17
            '==', '===' => 'Value must be equal to "{targetValueOrAttribute}".',
123 16
            '!=', '!==' => 'Value must not be equal to "{targetValueOrAttribute}".',
124 8
            '>' => 'Value must be greater than "{targetValueOrAttribute}".',
125 8
            '>=' => 'Value must be greater than or equal to "{targetValueOrAttribute}".',
126 8
            '<' => 'Value must be less than "{targetValueOrAttribute}".',
127 8
            '<=' => 'Value must be less than or equal to "{targetValueOrAttribute}".',
128 80
            default => throw new RuntimeException("Unknown operator: $this->operator."),
129
        };
130
    }
131
132 45
    #[ArrayShape([
133
        'targetValue' => 'mixed',
134
        'targetAttribute' => 'string|null',
135
        'message' => 'array',
136
        'type' => 'string',
137
        'operator' => 'string',
138
        'skipOnEmpty' => 'bool',
139
        'skipOnError' => 'bool',
140
    ])]
141
    public function getOptions(): array
142
    {
143
        return [
144 45
            'targetValue' => $this->targetValue,
145 45
            'targetAttribute' => $this->targetAttribute,
146
            'message' => [
147 45
                'message' => $this->getMessage(),
148
                'parameters' => [
149 45
                    'targetValue' => $this->targetValue,
150 45
                    'targetAttribute' => $this->targetAttribute,
151 45
                    'targetValueOrAttribute' => $this->targetValue ?? $this->targetAttribute,
152
                ],
153
            ],
154 45
            'type' => $this->type,
155 45
            'operator' => $this->operator,
156 45
            'skipOnEmpty' => $this->skipOnEmpty,
157 45
            'skipOnError' => $this->skipOnError,
158
        ];
159
    }
160
}
161