Passed
Pull Request — master (#364)
by
unknown
03:03
created

Compare::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.7666
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Closure;
8
use InvalidArgumentException;
9
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\ValidationContext;
16
use Yiisoft\Validator\WhenInterface;
17
18
abstract class Compare implements SerializableRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
19
{
20
    use SkipOnEmptyTrait;
21
    use SkipOnErrorTrait;
22
    use WhenTrait;
23
24
    /**
25
     * Constant for specifying the comparison as string values.
26
     * Values will be converted to strings before comparison.
27
     *
28
     * @see $type
29
     */
30
    public const TYPE_STRING = 'string';
31
    /**
32
     * Constant for specifying the comparison as numeric values.
33
     * Values will be converted to float numbers before comparison.
34
     *
35
     * @see $type
36
     */
37
    public const TYPE_NUMBER = 'number';
38
39
    private array $validOperatorsMap = [
40
        '==' => 1,
41
        '===' => 1,
42
        '!=' => 1,
43
        '!==' => 1,
44
        '>' => 1,
45
        '>=' => 1,
46
        '<' => 1,
47
        '<=' => 1,
48
    ];
49
50 33
    public function __construct(
51
        /**
52
         * @var scalar|null The constant value to be compared with. When both this property and {@see $targetAttribute}
53
         * are set, this property takes precedence.
54
         */
55
        private int|float|string|bool|null $targetValue = null,
56
        /**
57
         * @var string|null The name of the attribute to be compared with. When both this property and
58
         * {@see $targetValue} are set, the {@see $targetValue} takes precedence.
59
         */
60
        private string|null $targetAttribute = null,
61
        private string $incorrectDataSetTypeMessage = 'The attribute value returned from a custom data set must have ' .
62
        'a scalar type.',
63
        /**
64
         * @var string|null User-defined error message.
65
         */
66
        private string|null $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 in non-strict mode.
75
         * - `===`: check if two values are equal. The comparison is done in strict mode.
76
         * - `!=`: check if two values are NOT equal. The comparison is done in non-strict mode.
77
         * - `!==`: check if two values are NOT equal. The comparison is done in 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 {@see TYPE_NUMBER}.
84
         */
85
        private string $operator = '==',
86
        /**
87
         * @var bool|callable|null
88
         */
89
        private mixed $skipOnEmpty = null,
90
        private bool $skipOnError = false,
91
        /**
92
         * @var Closure(mixed, ValidationContext):bool|null
93
         */
94
        private ?Closure $when = null,
95
    ) {
96 33
        if (!isset($this->validOperatorsMap[$this->operator])) {
97
            throw new InvalidArgumentException("Operator \"$operator\" is not supported.");
98
        }
99
    }
100
101
    /**
102
     * @return scalar|null
103
     */
104 71
    public function getTargetValue(): int|float|string|bool|null
105
    {
106 71
        return $this->targetValue;
107
    }
108
109 71
    public function getTargetAttribute(): string|null
110
    {
111 71
        return $this->targetAttribute;
112
    }
113
114 71
    public function getType(): string
115
    {
116 71
        return $this->type;
117
    }
118
119 71
    public function getOperator(): string
120
    {
121 71
        return $this->operator;
122
    }
123
124
    public function getIncorrectDataSetTypeMessage(): string
125
    {
126
        return $this->incorrectDataSetTypeMessage;
127
    }
128
129 85
    public function getMessage(): string
130
    {
131 85
        return $this->message ?? match ($this->operator) {
132 22
            '==', '===' => 'Value must be equal to "{targetValueOrAttribute}".',
133 16
            '!=', '!==' => 'Value must not be equal to "{targetValueOrAttribute}".',
134 8
            '>' => 'Value must be greater than "{targetValueOrAttribute}".',
135 8
            '>=' => 'Value must be greater than or equal to "{targetValueOrAttribute}".',
136 8
            '<' => 'Value must be less than "{targetValueOrAttribute}".',
137 85
            '<=' => 'Value must be less than or equal to "{targetValueOrAttribute}".',
138
        };
139
    }
140
141 46
    public function getOptions(): array
142
    {
143
        return [
144 46
            'targetValue' => $this->targetValue,
145 46
            'targetAttribute' => $this->targetAttribute,
146
            'incorrectDataSetTypeMessage' => [
147 46
                'message' => $this->incorrectDataSetTypeMessage,
148
            ],
149
            'message' => [
150 46
                'message' => $this->getMessage(),
151
                'parameters' => [
152 46
                    'targetValue' => $this->targetValue,
153 46
                    'targetAttribute' => $this->targetAttribute,
154 46
                    'targetValueOrAttribute' => $this->targetValue ?? $this->targetAttribute,
155
                ],
156
            ],
157 46
            'type' => $this->type,
158 46
            'operator' => $this->operator,
159 46
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
160 46
            'skipOnError' => $this->skipOnError,
161
        ];
162
    }
163
164 71
    public function getHandlerClassName(): string
165
    {
166 71
        return CompareHandler::class;
167
    }
168
}
169