1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\DataSetInterface; |
8
|
|
|
use Yiisoft\Validator\ErrorMessage; |
9
|
|
|
use Yiisoft\Validator\HasValidationErrorMessage; |
10
|
|
|
use Yiisoft\Validator\Result; |
11
|
|
|
use Yiisoft\Validator\Rule; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* BooleanValidator checks if the attribute value is a boolean value or a value corresponding to it. |
15
|
|
|
*/ |
16
|
|
|
class Boolean extends Rule |
17
|
|
|
{ |
18
|
|
|
use HasValidationErrorMessage; |
19
|
|
|
/** |
20
|
|
|
* @var mixed the value representing true status. Defaults to '1'. |
21
|
|
|
*/ |
22
|
|
|
private $trueValue = '1'; |
23
|
|
|
/** |
24
|
|
|
* @var mixed the value representing false status. Defaults to '0'. |
25
|
|
|
*/ |
26
|
|
|
private $falseValue = '0'; |
27
|
|
|
/** |
28
|
|
|
* @var bool whether the comparison to [[trueValue]] and [[falseValue]] is strict. |
29
|
|
|
* When this is true, the attribute value and type must both match those of [[trueValue]] or [[falseValue]]. |
30
|
|
|
* Defaults to false, meaning only the value needs to be matched. |
31
|
|
|
*/ |
32
|
|
|
private bool $strict = false; |
33
|
|
|
|
34
|
|
|
private string $message = 'The value must be either "{true}" or "{false}".'; |
35
|
|
|
|
36
|
|
|
public function trueValue($value): self |
37
|
|
|
{ |
38
|
|
|
$new = clone $this; |
39
|
|
|
$new->trueValue = $value; |
40
|
|
|
return $new; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function falseValue($value): self |
44
|
|
|
{ |
45
|
|
|
$new = clone $this; |
46
|
|
|
$new->falseValue = $value; |
47
|
|
|
return $new; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function strict(bool $value): self |
51
|
|
|
{ |
52
|
|
|
$new = clone $this; |
53
|
|
|
$new->strict = $value; |
54
|
|
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
18 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
58
|
|
|
{ |
59
|
18 |
|
if ($this->strict) { |
60
|
8 |
|
$valid = $value === $this->trueValue || $value === $this->falseValue; |
61
|
|
|
} else { |
62
|
10 |
|
$valid = $value == $this->trueValue || $value == $this->falseValue; |
63
|
|
|
} |
64
|
|
|
|
65
|
18 |
|
$result = new Result(); |
66
|
|
|
|
67
|
18 |
|
if (!$valid) { |
68
|
8 |
|
$result->addError( |
69
|
8 |
|
new ErrorMessage( |
70
|
8 |
|
$this->message, |
71
|
|
|
[ |
72
|
8 |
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
73
|
8 |
|
'false' => $this->falseValue === false ? 'false' : $this->falseValue, |
74
|
|
|
] |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
18 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
public function getRawOptions(): array |
83
|
|
|
{ |
84
|
9 |
|
return array_merge( |
85
|
9 |
|
parent::getRawOptions(), |
86
|
|
|
[ |
87
|
9 |
|
'strict' => $this->strict, |
88
|
9 |
|
'trueValue' => $this->trueValue, |
89
|
9 |
|
'falseValue' => $this->falseValue, |
90
|
9 |
|
'message' => new ErrorMessage( |
91
|
9 |
|
$this->message, |
92
|
|
|
[ |
93
|
9 |
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
94
|
9 |
|
'false' => $this->falseValue === false ? 'false' : $this->falseValue, |
95
|
|
|
] |
96
|
|
|
), |
97
|
|
|
], |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|