-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathUniqueIterable.php
176 lines (164 loc) · 6.25 KB
/
UniqueIterable.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Rule;
use Attribute;
use Closure;
use JetBrains\PhpStorm\ArrayShape;
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;
/**
* Allows to define a set of rules for validating uniqueness of each element of an iterable.
*
* @see EachHandler Corresponding handler performing the actual validation.
*
* @psalm-import-type SkipOnEmptyValue from SkipOnEmptyInterface
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class UniqueIterable implements
DumpedRuleInterface,
SkipOnEmptyInterface,
SkipOnErrorInterface,
WhenInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
use WhenTrait;
/**
* @param string $incorrectInputMessage Error message used when validation fails because the validated value is not
* an iterable.
*
* 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 $incorrectItemValueMessage Error message used when validation fails because the validated iterable
* contains items with invalid values. Only the following types are allowed: scalar (string, integer, float,
* boolean), objects implementing `\Stringable` or `\DateTimeInterface` interfaces.
*
* You may use the following placeholders in the message:
*
* - `{property}`: the translated label of the property being validated.
* - `{type}`: the type of the iterable key being validated.
* @param string $differentTypesMessage Error message used when validation fails because the validated iterable
* contains items of different type.
*
* You may use the following placeholders in the message:
*
* - `{property}`: the translated label of the property being validated.
* @param string $message Error message used when validation fails because the validated iterable contains duplicate
* items.
*
* You may use the following placeholders in the message:
*
* - `{property}`: the translated label of the property being validated.
* @param bool|callable|null $skipOnEmpty Whether to skip this `Each` rule with all defined {@see $rules} if the
* validated value is empty / not passed. See {@see SkipOnEmptyInterface}.
* @param bool $skipOnError Whether to skip this `Each` rule with all defined {@see $rules} if any of the previous
* rules gave an error. See {@see SkipOnErrorInterface}.
* @param Closure|null $when A callable to define a condition for applying this `Each` rule with all defined
* {@see $rules}. See {@see WhenInterface}.
*
* @psalm-param SkipOnEmptyValue $skipOnEmpty
* @psalm-param WhenType $when
*/
public function __construct(
private string $incorrectInputMessage = '{Property} must be array or iterable.',
private string $incorrectItemValueMessage = 'The allowed types for iterable\'s item values of {property} are ' .
'integer, float, string, boolean and object implementing \Stringable or \DateTimeInterface.',
private string $differentTypesMessage = 'All iterable items of {property} must have the same type.',
private string $message = 'Every iterable\'s item of {property} must be unique.',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}
public function getName(): string
{
return self::class;
}
/**
* Gets error message used when validation fails because the validated value is not an iterable.
*
* @return string Error message / template.
*
* @see $incorrectInputMessage
*/
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
/**
* Error message used when validation fails because the validated iterable contains items with invalid values.
*
* @return string Error message / template.
*
* @see $incorrectItemValueMessage
*/
public function getIncorrectItemValueMessage(): string
{
return $this->incorrectItemValueMessage;
}
/**
* Error message used when validation fails because the validated iterable contains items of different types.
*
* @return string Error message / template.
*
* @see $differentTypesMessage
*/
public function getDifferentTypesMessage(): string
{
return $this->differentTypesMessage;
}
/**
* Error message used when validation fails because the validated iterable contains duplicate items.
*
* @return string Error message / template.
*
* @see $message
*/
public function getMessage(): string
{
return $this->message;
}
#[ArrayShape([
'incorrectInputMessage' => 'array',
'incorrectItemValueMessage' => 'array',
'message' => 'array',
'skipOnEmpty' => 'bool',
'skipOnError' => 'bool',
])]
public function getOptions(): array
{
return [
'incorrectInputMessage' => [
'template' => $this->incorrectInputMessage,
'parameters' => [],
],
'incorrectItemValueMessage' => [
'template' => $this->incorrectItemValueMessage,
'parameters' => [],
],
'differentTypesMessage' => [
'template' => $this->differentTypesMessage,
'parameters' => [],
],
'message' => [
'template' => $this->message,
'parameters' => [],
],
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
];
}
public function getHandler(): string
{
return UniqueIterableHandler::class;
}
}