|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Yiisoft\Validator\FormatterInterface; |
|
9
|
|
|
use Yiisoft\Validator\Result; |
|
10
|
|
|
use Yiisoft\Validator\Rule; |
|
11
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Checks if the value is a boolean value or a value corresponding to it. |
|
15
|
|
|
*/ |
|
16
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
|
17
|
|
|
final class Boolean extends Rule |
|
18
|
|
|
{ |
|
19
|
2 |
|
public function __construct( |
|
20
|
|
|
/** |
|
21
|
|
|
* @var mixed the value representing true status. |
|
22
|
|
|
*/ |
|
23
|
|
|
private mixed $trueValue = '1', |
|
24
|
|
|
/** |
|
25
|
|
|
* @var mixed the value representing false status. |
|
26
|
|
|
*/ |
|
27
|
|
|
private mixed $falseValue = '0', |
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict. |
|
30
|
|
|
* When this is `true`, the value and type must both match those of {@see $trueValue} or |
|
31
|
|
|
* {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched. |
|
32
|
|
|
*/ |
|
33
|
|
|
private bool $strict = false, |
|
34
|
|
|
private string $message = 'The value must be either "{true}" or "{false}".', |
|
35
|
|
|
?FormatterInterface $formatter = null, |
|
36
|
|
|
bool $skipOnEmpty = false, |
|
37
|
|
|
bool $skipOnError = false, |
|
38
|
|
|
$when = null |
|
39
|
|
|
) { |
|
40
|
2 |
|
parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
20 |
|
/** |
|
44
|
|
|
* @see $trueValue |
|
45
|
20 |
|
*/ |
|
46
|
8 |
|
public function trueValue(mixed $value): self |
|
47
|
|
|
{ |
|
48
|
12 |
|
$new = clone $this; |
|
49
|
|
|
$new->trueValue = $value; |
|
50
|
|
|
|
|
51
|
20 |
|
return $new; |
|
52
|
|
|
} |
|
53
|
20 |
|
|
|
54
|
13 |
|
/** |
|
55
|
|
|
* @see $falseValue |
|
56
|
|
|
*/ |
|
57
|
7 |
|
public function falseValue(mixed $value): self |
|
58
|
7 |
|
{ |
|
59
|
|
|
$new = clone $this; |
|
60
|
7 |
|
$new->falseValue = $value; |
|
61
|
|
|
|
|
62
|
|
|
return $new; |
|
63
|
14 |
|
} |
|
64
|
|
|
|
|
65
|
14 |
|
/** |
|
66
|
14 |
|
* @see $strict |
|
67
|
14 |
|
*/ |
|
68
|
|
|
public function strict(bool $value): self |
|
69
|
|
|
{ |
|
70
|
|
|
$new = clone $this; |
|
71
|
7 |
|
$new->strict = $value; |
|
72
|
|
|
|
|
73
|
7 |
|
return $new; |
|
74
|
7 |
|
} |
|
75
|
7 |
|
|
|
76
|
7 |
|
/** |
|
77
|
7 |
|
* @see $message |
|
78
|
|
|
*/ |
|
79
|
|
|
public function message(string $value): self |
|
80
|
|
|
{ |
|
81
|
|
|
$new = clone $this; |
|
82
|
|
|
$new->message = $value; |
|
83
|
|
|
|
|
84
|
|
|
return $new; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function validateValue($value, ?ValidationContext $context = null): Result |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->strict) { |
|
90
|
|
|
$valid = $value === $this->trueValue || $value === $this->falseValue; |
|
91
|
|
|
} else { |
|
92
|
|
|
$valid = $value == $this->trueValue || $value == $this->falseValue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$result = new Result(); |
|
96
|
|
|
|
|
97
|
|
|
if ($valid) { |
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$message = $this->getFormattedMessage(); |
|
102
|
|
|
$result->addError($message); |
|
103
|
|
|
|
|
104
|
|
|
return $result; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function getFormattedMessage(): string |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->formatMessage($this->message, [ |
|
110
|
|
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
|
111
|
|
|
'false' => $this->falseValue === false ? 'false' : $this->falseValue, |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getOptions(): array |
|
116
|
|
|
{ |
|
117
|
|
|
return array_merge(parent::getOptions(), [ |
|
118
|
|
|
'trueValue' => $this->trueValue, |
|
119
|
|
|
'falseValue' => $this->falseValue, |
|
120
|
|
|
'strict' => $this->strict, |
|
121
|
|
|
'message' => $this->getFormattedMessage(), |
|
122
|
|
|
]); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|