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\DumpedRuleInterface; |
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
15
|
|
|
use Yiisoft\Validator\WhenInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Defines validation options to check that one of specified attributes is filled. |
19
|
|
|
* |
20
|
|
|
* Both arrays and objects with public properties are supported as validated values. |
21
|
|
|
* |
22
|
|
|
* @see OneOfHandler |
23
|
|
|
* |
24
|
|
|
* @psalm-import-type WhenType from WhenInterface |
25
|
|
|
*/ |
26
|
|
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
27
|
|
|
final class OneOf implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
28
|
|
|
{ |
29
|
|
|
use SkipOnEmptyTrait; |
30
|
|
|
use SkipOnErrorTrait; |
31
|
|
|
use WhenTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string[] $attributes The list of required attributes that will be checked. |
35
|
|
|
* @param string $incorrectInputMessage A message used when the input is incorrect. |
36
|
|
|
* |
37
|
|
|
* You may use the following placeholders in the message: |
38
|
|
|
* |
39
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
40
|
|
|
* - `{type}`: the type of the value being validated. |
41
|
|
|
* @param string $message A message used when the value is not valid. |
42
|
|
|
* |
43
|
|
|
* You may use the following placeholders in the message: |
44
|
|
|
* |
45
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
46
|
|
|
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. |
47
|
|
|
* See {@see SkipOnEmptyInterface}. |
48
|
|
|
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. |
49
|
|
|
* See {@see SkipOnErrorInterface}. |
50
|
|
|
* @param Closure|null $when A callable to define a condition for applying the rule. |
51
|
|
|
* See {@see WhenInterface}. |
52
|
|
|
* |
53
|
|
|
* @psalm-param WhenType $when |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
private array $attributes, |
57
|
|
|
private string $incorrectInputMessage = 'The value must be an array or an object.', |
58
|
|
|
private string $message = 'Exactly 1 attribute from this list must be filled: {attributes}.', |
59
|
|
|
private mixed $skipOnEmpty = null, |
60
|
|
|
private bool $skipOnError = false, |
61
|
|
|
private Closure|null $when = null |
62
|
|
|
) { |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getName(): string |
66
|
|
|
{ |
67
|
|
|
return self::class; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the list of required attributes that will be checked. |
72
|
|
|
* |
73
|
|
|
* @return string[] The list of attributes. |
74
|
|
|
* |
75
|
|
|
* @see $attributes |
76
|
|
|
*/ |
77
|
|
|
public function getAttributes(): array |
78
|
|
|
{ |
79
|
|
|
return $this->attributes; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the message used when the input is incorrect. |
84
|
|
|
* |
85
|
|
|
* @return string Error message. |
86
|
|
|
* |
87
|
|
|
* @see $incorrectInputMessage |
88
|
|
|
*/ |
89
|
|
|
public function getIncorrectInputMessage(): string |
90
|
|
|
{ |
91
|
|
|
return $this->incorrectInputMessage; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the message used when the value is not valid. |
96
|
|
|
* |
97
|
|
|
* @return string Error message. |
98
|
|
|
* |
99
|
|
|
* @see $message |
100
|
|
|
*/ |
101
|
|
|
public function getMessage(): string |
102
|
|
|
{ |
103
|
|
|
return $this->message; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getOptions(): array |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
'attributes' => $this->attributes, |
110
|
|
|
'incorrectInputMessage' => [ |
111
|
|
|
'template' => $this->incorrectInputMessage, |
112
|
|
|
'parameters' => [], |
113
|
|
|
], |
114
|
|
|
'message' => [ |
115
|
|
|
'template' => $this->message, |
116
|
|
|
'parameters' => [], |
117
|
|
|
], |
118
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
119
|
|
|
'skipOnError' => $this->skipOnError, |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getHandler(): string |
124
|
|
|
{ |
125
|
|
|
return OneOfHandler::class; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|