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\SerializableRuleInterface; |
10
|
|
|
use Yiisoft\Validator\BeforeValidationInterface; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; |
12
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
13
|
|
|
use Yiisoft\Validator\ValidationContext; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Checks if the value is a "true" boolean value or a value corresponding to it. Useful for user agreements etc. |
17
|
|
|
* |
18
|
|
|
* @see IsTrueHandler |
19
|
|
|
*/ |
20
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
21
|
|
|
final class IsTrue implements SerializableRuleInterface, BeforeValidationInterface |
22
|
|
|
{ |
23
|
|
|
use BeforeValidationTrait; |
24
|
|
|
use RuleNameTrait; |
25
|
|
|
|
26
|
2 |
|
public function __construct( |
27
|
|
|
/** |
28
|
|
|
* @var mixed the value representing "true" status. Defaults to `1`. |
29
|
|
|
*/ |
30
|
|
|
private mixed $trueValue = '1', |
31
|
|
|
/** |
32
|
|
|
* @var bool whether the comparison to {@see $trueValue} is strict. When this is "true", the value and type must |
33
|
|
|
* both match {@see $trueValue}. Defaults to "false", meaning only the value needs to be matched. |
34
|
|
|
*/ |
35
|
|
|
private bool $strict = false, |
36
|
|
|
private string $message = 'The value must be "{true}".', |
37
|
|
|
private bool $skipOnEmpty = false, |
38
|
|
|
private bool $skipOnError = false, |
39
|
|
|
/** |
40
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
41
|
|
|
*/ |
42
|
|
|
private ?Closure $when = null, |
43
|
|
|
) { |
44
|
|
|
} |
45
|
|
|
|
46
|
15 |
|
public function getTrueValue(): mixed |
47
|
|
|
{ |
48
|
15 |
|
return $this->trueValue; |
49
|
|
|
} |
50
|
|
|
|
51
|
15 |
|
public function isStrict(): bool |
52
|
|
|
{ |
53
|
15 |
|
return $this->strict; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
public function getMessage(): string |
57
|
|
|
{ |
58
|
11 |
|
return $this->message; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function getOptions(): array |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
2 |
|
'trueValue' => $this->trueValue, |
65
|
2 |
|
'strict' => $this->strict, |
66
|
|
|
'message' => [ |
67
|
2 |
|
'message' => $this->message, |
68
|
|
|
'parameters' => [ |
69
|
2 |
|
'true' => $this->trueValue === true ? '1' : $this->trueValue, |
70
|
|
|
], |
71
|
|
|
], |
72
|
2 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
73
|
2 |
|
'skipOnError' => $this->skipOnError, |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function getHandlerClassName(): string |
78
|
|
|
{ |
79
|
1 |
|
return IsTrueHandler::class; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|