|
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 "true" boolean value or a value corresponding to it. Useful for user agreements etc. |
|
19
|
|
|
* |
|
20
|
|
|
* @see IsTrueHandler |
|
21
|
|
|
* |
|
22
|
|
|
* @psalm-import-type WhenType from WhenInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
25
|
|
|
final class IsTrue implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use SkipOnEmptyTrait; |
|
28
|
|
|
use SkipOnErrorTrait; |
|
29
|
|
|
use WhenTrait; |
|
30
|
4 |
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @const Default message used for all cases. |
|
33
|
|
|
*/ |
|
34
|
|
|
private const DEFAULT_MESSAGE = 'The value must be "{true}".'; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
private int|float|string|bool $trueValue = '1', |
|
38
|
|
|
private bool $strict = false, |
|
39
|
|
|
private string $messageWithType = self::DEFAULT_MESSAGE, |
|
40
|
|
|
private string $messageWithValue = self::DEFAULT_MESSAGE, |
|
41
|
|
|
private mixed $skipOnEmpty = null, |
|
42
|
|
|
private bool $skipOnError = false, |
|
43
|
|
|
private Closure|null $when = null, |
|
44
|
|
|
) { |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getName(): string |
|
48
|
|
|
{ |
|
49
|
|
|
return 'isTrue'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getTrueValue(): int|float|string|bool |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->trueValue; |
|
55
|
|
|
} |
|
56
|
1 |
|
|
|
57
|
|
|
public function isStrict(): bool |
|
58
|
|
|
{ |
|
59
|
16 |
|
return $this->strict; |
|
60
|
|
|
} |
|
61
|
16 |
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* A getter for {@see $messageWithType}. |
|
64
|
16 |
|
* |
|
65
|
|
|
* @return string Error message. |
|
66
|
16 |
|
*/ |
|
67
|
|
|
public function getMessageWithType(): string |
|
68
|
|
|
{ |
|
69
|
12 |
|
return $this->messageWithType; |
|
70
|
|
|
} |
|
71
|
12 |
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* A getter for {@see $messageWithValue}. |
|
74
|
3 |
|
* |
|
75
|
|
|
* @return string Error message. |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function getMessageWithValue(): string |
|
78
|
3 |
|
{ |
|
79
|
|
|
return $this->messageWithValue; |
|
80
|
3 |
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
public function getOptions(): array |
|
83
|
|
|
{ |
|
84
|
|
|
$messageParameters = [ |
|
85
|
3 |
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
|
86
|
3 |
|
]; |
|
87
|
|
|
|
|
88
|
|
|
return [ |
|
89
|
|
|
'trueValue' => $this->trueValue, |
|
90
|
16 |
|
'strict' => $this->strict, |
|
91
|
|
|
'messageWithType' => [ |
|
92
|
16 |
|
'template' => $this->messageWithType, |
|
93
|
|
|
'parameters' => $messageParameters, |
|
94
|
|
|
], |
|
95
|
|
|
'messageWithValue' => [ |
|
96
|
|
|
'template' => $this->messageWithValue, |
|
97
|
|
|
'parameters' => $messageParameters, |
|
98
|
|
|
], |
|
99
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
100
|
|
|
'skipOnError' => $this->skipOnError, |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getHandler(): string |
|
105
|
|
|
{ |
|
106
|
|
|
return IsTrueHandler::class; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|