|
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\SerializableRuleInterface; |
|
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
15
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
16
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Checks if the value is a boolean value or a value corresponding to it. |
|
20
|
|
|
*/ |
|
21
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
22
|
|
|
final class Boolean implements SerializableRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use SkipOnEmptyTrait; |
|
25
|
|
|
use SkipOnErrorTrait; |
|
26
|
|
|
use WhenTrait; |
|
27
|
|
|
|
|
28
|
6 |
|
public function __construct( |
|
29
|
|
|
/** |
|
30
|
|
|
* @var scalar the value representing "true" status. Defaults to `1`. |
|
31
|
|
|
*/ |
|
32
|
|
|
private int|float|string|bool $trueValue = '1', |
|
33
|
|
|
/** |
|
34
|
|
|
* @var scalar the value representing "false" status. Defaults to `0`. |
|
35
|
|
|
*/ |
|
36
|
|
|
private int|float|string|bool $falseValue = '0', |
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict. When this is `true`, |
|
39
|
|
|
* the value and type must both match those of {@see $trueValue} or {@see $falseValue}. Defaults to `false`, |
|
40
|
|
|
* meaning only the value needs to be matched. |
|
41
|
|
|
*/ |
|
42
|
|
|
private bool $strict = false, |
|
43
|
|
|
private string $nonScalarMessage = 'Value must be either "{true}" or "{false}".', |
|
44
|
|
|
private string $scalarMessage = 'Value must be either "{true}" or "{false}".', |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var bool|callable|null |
|
48
|
|
|
*/ |
|
49
|
|
|
private $skipOnEmpty = null, |
|
50
|
|
|
private bool $skipOnError = false, |
|
51
|
|
|
/** |
|
52
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
|
53
|
|
|
*/ |
|
54
|
|
|
private ?Closure $when = null, |
|
55
|
|
|
) { |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function getName(): string |
|
59
|
|
|
{ |
|
60
|
1 |
|
return 'boolean'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return scalar |
|
65
|
|
|
*/ |
|
66
|
21 |
|
public function getTrueValue(): int|float|string|bool |
|
67
|
|
|
{ |
|
68
|
21 |
|
return $this->trueValue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return scalar |
|
73
|
|
|
*/ |
|
74
|
12 |
|
public function getFalseValue(): int|float|string|bool |
|
75
|
|
|
{ |
|
76
|
12 |
|
return $this->falseValue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
18 |
|
public function isStrict(): bool |
|
80
|
|
|
{ |
|
81
|
18 |
|
return $this->strict; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
public function getNonScalarMessage(): string |
|
85
|
|
|
{ |
|
86
|
3 |
|
return $this->nonScalarMessage; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
public function getScalarMessage(): string |
|
90
|
|
|
{ |
|
91
|
5 |
|
return $this->scalarMessage; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
public function getOptions(): array |
|
95
|
|
|
{ |
|
96
|
3 |
|
$messageParameters = [ |
|
97
|
3 |
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
|
98
|
3 |
|
'false' => $this->falseValue === false ? 'false' : $this->falseValue, |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
return [ |
|
102
|
3 |
|
'trueValue' => $this->trueValue, |
|
103
|
3 |
|
'falseValue' => $this->falseValue, |
|
104
|
3 |
|
'strict' => $this->strict, |
|
105
|
|
|
'nonScalarMessage' => [ |
|
106
|
3 |
|
'message' => $this->nonScalarMessage, |
|
107
|
|
|
'parameters' => $messageParameters, |
|
108
|
|
|
], |
|
109
|
|
|
'message' => [ |
|
110
|
3 |
|
'message' => $this->scalarMessage, |
|
111
|
|
|
'parameters' => $messageParameters, |
|
112
|
|
|
], |
|
113
|
3 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
114
|
3 |
|
'skipOnError' => $this->skipOnError, |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
21 |
|
public function getHandlerClassName(): string |
|
119
|
|
|
{ |
|
120
|
21 |
|
return BooleanHandler::class; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|