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