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