-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorChain.php
More file actions
252 lines (226 loc) · 7.89 KB
/
ValidatorChain.php
File metadata and controls
252 lines (226 loc) · 7.89 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* This file is part of the Vection package.
*
* (c) David M. Lung <vection@davidlung.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Vection\Component\Validator;
use Vection\Component\Validator\Validator\Color;
use Vection\Component\Validator\Validator\IsRequired;
use Vection\Component\Validator\Validator\Locale;
use Vection\Component\Validator\Validator\TypedArray;
use Vection\Contracts\Validator\ValidatorChainInterface;
use Vection\Contracts\Validator\ValidatorInterface;
use Vection\Contracts\Validator\ViolationInterface;
/**
* Class ValidationChain
*
* This class builds a chain of validator instances.
* Each validation method creates an specific instance of a validator.
* This validator chain provides the possibility to validate one or more
* values by multiple validators at once.
*
* @package Vection\Component\Validator
*
* @method ValidatorChain alphaNumeric()
* @method ValidatorChain arraySchema(array $schema, int $maxTemplateRecursion = 3)
* @method ValidatorChain betweenValue(int $min, int $max)
* @method ValidatorChain betweenLength(int $min, int $max)
* @method ValidatorChain boolean()
* @method ValidatorChain color(int $format = Color::HEX)
* @method ValidatorChain contains(array $needle)
* @method ValidatorChain date(string $format)
* @method ValidatorChain digit()
* @method ValidatorChain directory()
* @method ValidatorChain domain()
* @method ValidatorChain email()
* @method ValidatorChain endsWith(string $needle)
* @method ValidatorChain equals($value)
* @method ValidatorChain file()
* @method ValidatorChain greaterOrEqualThan(int $limit)
* @method ValidatorChain greaterThan(int $limit)
* @method ValidatorChain hex()
* @method ValidatorChain hostname()
* @method ValidatorChain iban()
* @method ValidatorChain iconName(string $prefix = null, int $maxLength = null)
* @method ValidatorChain inArray(array $haystack)
* @method ValidatorChain integer()
* @method ValidatorChain isArray()
* @method ValidatorChain isRequired()
* @method ValidatorChain isString()
* @method ValidatorChain ipv4()
* @method ValidatorChain ipv6()
* @method ValidatorChain length(int $length)
* @method ValidatorChain lessOrEqualThan(int $limit)
* @method ValidatorChain lessThan(int $limit)
* @method ValidatorChain locale(bool $strict = true, string $separator = Locale::HYPHEN)
* @method ValidatorChain max(int $value)
* @method ValidatorChain maxLength(int $length)
* @method ValidatorChain mimeType(bool $includeWildcard = false)
* @method ValidatorChain min(int $limit)
* @method ValidatorChain minLength(int $limit)
* @method ValidatorChain notBlank()
* @method ValidatorChain notEquals($value)
* @method ValidatorChain notInArray(array $haystack)
* @method ValidatorChain notEmpty()
* @method ValidatorChain notNull()
* @method ValidatorChain null()
* @method ValidatorChain nullable()
* @method ValidatorChain numeric()
* @method ValidatorChain optional()
* @method ValidatorChain phone()
* @method ValidatorChain phoneE164()
* @method ValidatorChain range(int $min, int $max)
* @method ValidatorChain regex(string $pattern)
* @method ValidatorChain startsWith(string $needle)
* @method ValidatorChain subdomain()
* @method ValidatorChain timezone(int $group = null, string $countryCode = null)
* @method ValidatorChain typedArray(int $type = TypedArray::STRING)
* @method ValidatorChain url(bool $path = false, bool $query = false)
* @method ValidatorChain uuid()
*/
class ValidatorChain implements ValidatorChainInterface
{
/**
* The factory which creating validator objects by its names.
*
* @var ValidatorFactory
*/
protected ValidatorFactory $validatorFactory;
/**
* This property contains assertion definition.
* Each entry can have multiple assertion with
* different parameters.
*
* @var ValidatorInterface[][]
*/
protected array $chain = [];
/**
* This property contains all violation objects
* that are collected during the verify method.
*
* @var ViolationInterface[]
*/
protected array $violations = [];
/**
* Contains the mapping of given data value names and
* the virtual validator "nullable" to determine the null
* values that can be skipped by defined nullable validator.
*
* @var int[]
*/
protected array $nullable = [];
/**
* Contains the mapping of given data value names and
* the virtual validator "optional" to determine the optional
* values that can be skipped if payload does not contains the expected data..
*
* @var int[]
*/
protected array $optional = [];
/**
* Creates a new validator factory.
*/
public function __construct()
{
$this->validatorFactory = new ValidatorFactory();
}
/**
* Adds a key for which all following assertion will be set.
*
* @param string ...$keys
*
* @return ValidatorChain
*/
public function __invoke(string ...$keys): ValidatorChain
{
# Add support for multi dimension array values!
$this->chain[$keys[0]] = [
# All definition are required by default
new IsRequired()
];
end($this->chain);
return $this;
}
/**
* This magic method is used to accept several validation
* function which will be process later in the Validator.
*
* @param string $name
* @param mixed[] $constraints
*
* @return ValidatorChain
*/
public function __call(string $name, array $constraints = []): ValidatorChain
{
if ($name === 'nullable') {
# This is a virtual validator that marks the subject as nullable
$this->nullable[key($this->chain)] = 1;
return $this;
}
if ($name === 'optional') {
# This is a virtual validator that marks the subject as optional
$this->optional[key($this->chain)] = 1;
return $this;
}
# Create the validator object and save for current subject
$validator = $this->validatorFactory->create($name, $constraints);
return $this->use($validator);
}
/**
* Registers a custom validator with its constraints.
*
* @param ValidatorInterface $validator
*
* @return ValidatorChain
*/
public function use(ValidatorInterface $validator): ValidatorChain
{
$this->chain[key($this->chain)][] = $validator;
return $this;
}
/**
* @inheritDoc
*
* @return ViolationInterface[]
*/
public function getViolations(): array
{
return $this->violations;
}
/**
* @inheritDoc
*/
public function verify(array $data): void
{
foreach ( $this->chain as $subject => $validators ) {
# First we have to know the current value
$value = ($data[$subject] ?? null);
$valueExists = array_key_exists($subject, $data);
if (!$valueExists && isset($this->optional[$subject])) {
continue;
}
# Skip if value is null and validate against nullable
if ( ($value === null || ! $validators) && isset($this->nullable[$subject]) && $valueExists ) {
continue;
}
foreach ( $validators as $validator ) {
if ($validator instanceof IsRequired) {
$violation = $validator->validate($valueExists, $subject);
}
else {
$violation = $validator->validate($value, $subject);
}
if ( $violation ) {
# The value is invalid so take the violation and continue to next subject
$this->violations[] = $violation;
continue 2;
}
}
}
}
}