1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
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\RuleWithOptionsInterface; |
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
15
|
|
|
use Yiisoft\Validator\WhenInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A variation of {@see Boolean} rule limiting the allowed values to "true" only (not limited to boolean "true" type). |
19
|
|
|
* What value exactly is considered "true" can be configured via {@see $trueValue} setting. There is also an option to |
20
|
|
|
* choose between strict and non-strict mode of comparison (see {@see $strict}). |
21
|
|
|
* |
22
|
|
|
* A typical scope of application is a user agreement. If the purpose is to also check the falsiness, use {@see Boolean} |
23
|
|
|
* rule instead. |
24
|
|
|
* |
25
|
|
|
* @see IsTrueHandler Corresponding handler performing the actual validation. |
26
|
|
|
* |
27
|
|
|
* @psalm-import-type WhenType from WhenInterface |
28
|
|
|
*/ |
29
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
30
|
4 |
|
final class IsTrue implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
31
|
|
|
{ |
32
|
|
|
use SkipOnEmptyTrait; |
33
|
|
|
use SkipOnErrorTrait; |
34
|
|
|
use WhenTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @const Default message used for all cases. |
38
|
|
|
*/ |
39
|
|
|
private const DEFAULT_MESSAGE = 'The value must be "{true}".'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param scalar $trueValue The value that is considered to be "true". Only scalar values (either int, float, string |
43
|
|
|
* or bool) are allowed. Defaults to `1` string. |
44
|
|
|
* @param bool $strict Whether the comparison to {@see $trueValue} is strict: |
45
|
|
|
* |
46
|
|
|
* - Strict mode uses `===` operator meaning the type and the value must both match to those set in |
47
|
|
|
* {@see $trueValue}. |
48
|
|
|
* - Non-strict mode uses `==` operator meaning that type juggling is performed first before the comparison. You can |
49
|
|
|
* read more in the PHP docs: |
50
|
|
|
* |
51
|
|
|
* - https://www.php.net/manual/en/language.operators.comparison.php |
52
|
|
|
* - https://www.php.net/manual/en/types.comparisons.php |
53
|
|
|
* - https://www.php.net/manual/en/language.types.type-juggling.php |
54
|
1 |
|
* |
55
|
|
|
* Defaults to `false` meaning non-strict mode is used. |
56
|
1 |
|
* @param string $messageWithType Error message used when validation fails and neither non-scalar value (int, |
57
|
|
|
* float, string, bool) nor null was provided as input. The type is used instead of value here for more predictable |
58
|
|
|
* formatting. |
59
|
16 |
|
* |
60
|
|
|
* You may use the following placeholders in the message: |
61
|
16 |
|
* |
62
|
|
|
* - `{attribute}`: the label of the attribute being validated. |
63
|
|
|
* - `{true}`: the value set in {@see $trueValue} option. |
64
|
16 |
|
* - `{type}`: the type of the value being validated. |
65
|
|
|
* @param string $messageWithValue Error message used when validation fails and either scalar value (int, float, |
66
|
16 |
|
* string, bool) or null was provided as input. |
67
|
|
|
* |
68
|
|
|
* You may use the following placeholders in the message: |
69
|
12 |
|
* |
70
|
|
|
* - `{attribute}`: the label of the attribute being validated. |
71
|
12 |
|
* - `{true}`: the value set in {@see $trueValue} option. |
72
|
|
|
* - `{value}`: the value being validated. |
73
|
|
|
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty / not passed. |
74
|
3 |
|
* See {@see SkipOnEmptyInterface}. |
75
|
|
|
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See |
76
|
|
|
* {@see SkipOnErrorInterface}. |
77
|
3 |
|
* @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}. |
78
|
3 |
|
* @psalm-param WhenType $when |
79
|
|
|
*/ |
80
|
3 |
|
public function __construct( |
81
|
|
|
private int|float|string|bool $trueValue = '1', |
82
|
3 |
|
private bool $strict = false, |
83
|
|
|
private string $messageWithType = self::DEFAULT_MESSAGE, |
84
|
|
|
private string $messageWithValue = self::DEFAULT_MESSAGE, |
85
|
3 |
|
private mixed $skipOnEmpty = null, |
86
|
3 |
|
private bool $skipOnError = false, |
87
|
|
|
private Closure|null $when = null, |
88
|
|
|
) { |
89
|
|
|
} |
90
|
16 |
|
|
91
|
|
|
public function getName(): string |
92
|
16 |
|
{ |
93
|
|
|
return 'isTrue'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the value that is considered to be "true". |
98
|
|
|
* |
99
|
|
|
* @return scalar A scalar value. |
100
|
|
|
* |
101
|
|
|
* @see $trueValue |
102
|
|
|
*/ |
103
|
|
|
public function getTrueValue(): int|float|string|bool |
104
|
|
|
{ |
105
|
|
|
return $this->trueValue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Whether the comparison to {@see $trueValue} is strict. |
110
|
|
|
* |
111
|
|
|
* @return bool `true` - strict, `false` - non-strict. |
112
|
|
|
* |
113
|
|
|
* @see $strict |
114
|
|
|
*/ |
115
|
|
|
public function isStrict(): bool |
116
|
|
|
{ |
117
|
|
|
return $this->strict; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets error message used when validation fails and value is complex to format, so its type is used instead. |
122
|
|
|
* |
123
|
|
|
* @return string Error message / template. |
124
|
|
|
* |
125
|
|
|
* @see $messageWithType |
126
|
|
|
*/ |
127
|
|
|
public function getMessageWithType(): string |
128
|
|
|
{ |
129
|
|
|
return $this->messageWithType; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets error message used when validation fails and value can be formatted. |
134
|
|
|
* |
135
|
|
|
* @return string Error message / template. |
136
|
|
|
* |
137
|
|
|
* @see $messageWithValue |
138
|
|
|
*/ |
139
|
|
|
public function getMessageWithValue(): string |
140
|
|
|
{ |
141
|
|
|
return $this->messageWithValue; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getOptions(): array |
145
|
|
|
{ |
146
|
|
|
$messageParameters = [ |
147
|
|
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
return [ |
151
|
|
|
'trueValue' => $this->trueValue, |
152
|
|
|
'strict' => $this->strict, |
153
|
|
|
'messageWithType' => [ |
154
|
|
|
'template' => $this->messageWithType, |
155
|
|
|
'parameters' => $messageParameters, |
156
|
|
|
], |
157
|
|
|
'messageWithValue' => [ |
158
|
|
|
'template' => $this->messageWithValue, |
159
|
|
|
'parameters' => $messageParameters, |
160
|
|
|
], |
161
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
162
|
|
|
'skipOnError' => $this->skipOnError, |
163
|
|
|
]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getHandler(): string |
167
|
|
|
{ |
168
|
|
|
return IsTrueHandler::class; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|