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\RuleNameTrait; |
|
|
|
|
10
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
11
|
|
|
use Yiisoft\Validator\RuleInterface; |
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 implements RuleInterface |
18
|
|
|
{ |
19
|
|
|
use HandlerClassNameTrait; |
20
|
|
|
use RuleNameTrait; |
21
|
|
|
|
22
|
1 |
|
public function __construct( |
23
|
|
|
/** |
24
|
|
|
* @var mixed the value representing true status. Defaults to '1'. |
25
|
|
|
*/ |
26
|
|
|
public $trueValue = '1', |
27
|
|
|
/** |
28
|
|
|
* @var mixed the value representing false status. Defaults to '0'. |
29
|
|
|
*/ |
30
|
|
|
public $falseValue = '0', |
31
|
|
|
/** |
32
|
|
|
* @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict. |
33
|
|
|
* When this is `true`, the value and type must both match those of {@see $trueValue} or |
34
|
|
|
* {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched. |
35
|
|
|
*/ |
36
|
|
|
public bool $strict = false, |
37
|
|
|
public string $message = 'The value must be either "{true}" or "{false}".', |
38
|
|
|
public bool $skipOnEmpty = false, |
39
|
|
|
public bool $skipOnError = false, |
40
|
|
|
public ?Closure $when = null, |
41
|
|
|
) { |
42
|
|
|
} |
43
|
|
|
|
44
|
7 |
|
public function getOptions(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
7 |
|
'trueValue' => $this->trueValue, |
48
|
7 |
|
'falseValue' => $this->falseValue, |
49
|
7 |
|
'strict' => $this->strict, |
50
|
|
|
'message' => [ |
51
|
7 |
|
'message' => $this->message, |
52
|
|
|
'parameters' => [ |
53
|
|
|
// TODO: get reasons to do like this |
54
|
|
|
// 'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
55
|
|
|
// 'false' => $this->falseValue === false ? 'false' : $this->falseValue, |
56
|
7 |
|
'true' => $this->trueValue, |
57
|
7 |
|
'false' => $this->falseValue, |
58
|
|
|
], |
59
|
|
|
], |
60
|
7 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
61
|
7 |
|
'skipOnError' => $this->skipOnError, |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|