-
Notifications
You must be signed in to change notification settings - Fork 23
/
Validator.php
341 lines (299 loc) · 7.93 KB
/
Validator.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
namespace Awurth\SlimValidation;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface as Request;
use ReflectionClass;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Rules\AbstractComposite;
use Respect\Validation\Validator as V;
/**
* Validator.
*
* @author Alexis Wurth <alexis.wurth57@gmail.com>
*/
class Validator
{
/**
* The validated data.
*
* @var array
*/
protected $data;
/**
* The default error messages for the given rules.
*
* @var array
*/
protected $defaultMessages;
/**
* The list of validation errors.
*
* @var array
*/
protected $errors;
/**
* Tells if errors should be stored in an associative array
* where the key is the name of the validation rule.
*
* @var bool
*/
protected $storeErrorsWithRules;
/**
* Constructor.
*
* @param bool $storeErrorsWithRules
* @param array $defaultMessages
*/
public function __construct($storeErrorsWithRules = true, array $defaultMessages = [])
{
$this->storeErrorsWithRules = $storeErrorsWithRules;
$this->defaultMessages = $defaultMessages;
}
/**
* Tells if there is no error.
*
* @return bool
*/
public function isValid()
{
return empty($this->errors);
}
/**
* Validates request parameters with the given rules.
*
* @param Request $request
* @param array $rules
* @param array $messages
*
* @return $this
*/
public function validate(Request $request, array $rules, array $messages = [])
{
foreach ($rules as $param => $options) {
$value = $this->getRequestParam($request, $param);
$this->data[$param] = $value;
$isRule = $options instanceof V;
try {
if ($isRule) {
$options->assert($value);
} else {
if (!isset($options['rules']) || !($options['rules'] instanceof V)) {
throw new InvalidArgumentException('Validation rules are missing');
}
$options['rules']->assert($value);
}
} catch (NestedValidationException $e) {
// If the 'message' key exists, set it as only message for this param
if (!$isRule && isset($options['message']) && is_string($options['message'])) {
$this->errors[$param] = [$options['message']];
return $this;
} else {
// If the 'messages' key exists, override global messages
$this->setMessages($e, $param, $options, $isRule);
}
}
}
return $this;
}
/**
* Adds an error for a parameter.
*
* @param string $param
* @param string $message
*
* @return $this
*/
public function addError($param, $message)
{
$this->errors[$param][] = $message;
return $this;
}
/**
* Adds errors for a parameter.
*
* @param string $param
* @param string[] $messages
*
* @return $this
*/
public function addErrors($param, array $messages)
{
foreach ($messages as $message) {
$this->errors[$param][] = $message;
}
return $this;
}
/**
* Gets the validated data.
*
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Gets all errors.
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* Gets the first error of a parameter.
*
* @param string $param
*
* @return string
*/
public function getFirstError($param)
{
if (isset($this->errors[$param])) {
$first = array_slice($this->errors[$param], 0, 1);
return array_shift($first);
}
return '';
}
/**
* Gets errors of a parameter.
*
* @param string $param
*
* @return array
*/
public function getParamErrors($param)
{
return isset($this->errors[$param]) ? $this->errors[$param] : [];
}
/**
* Gets the error of a validation rule for a parameter.
*
* @param string $param
* @param string $rule
*
* @return string
*/
public function getParamRuleError($param, $rule)
{
return isset($this->errors[$param][$rule]) ? $this->errors[$param][$rule] : '';
}
/**
* Gets the value of a parameter in validated data.
*
* @param string $param
*
* @return string
*/
public function getValue($param)
{
return isset($this->data[$param]) ? $this->data[$param] : '';
}
/**
* Sets the validator data.
*
* @param array $data
*
* @return $this
*/
public function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* Sets all errors.
*
* @param array $errors
*
* @return $this
*/
public function setErrors(array $errors)
{
$this->errors = $errors;
return $this;
}
/**
* Sets the errors of a parameter.
*
* @param string $param
* @param array $errors
*
* @return $this
*/
public function setParamErrors($param, array $errors)
{
$this->errors[$param] = $errors;
return $this;
}
/**
* Sets the value of parameters.
*
* @param array $data
*
* @return $this
*/
public function setValues(array $data)
{
$this->data = array_merge($this->data, $data);
return $this;
}
/**
* Fetch request parameter value from body or query string (in that order).
*
* @param Request $request
* @param string $key The parameter key.
* @param string $default The default value.
*
* @return mixed The parameter value.
*/
protected function getRequestParam(Request $request, $key, $default = null)
{
$postParams = $request->getParsedBody();
$getParams = $request->getQueryParams();
$result = $default;
if (is_array($postParams) && isset($postParams[$key])) {
$result = $postParams[$key];
} elseif (is_object($postParams) && property_exists($postParams, $key)) {
$result = $postParams->$key;
} elseif (isset($getParams[$key])) {
$result = $getParams[$key];
}
return $result;
}
/**
* Sets error messages after validation
*
* @param NestedValidationException $e
* @param string $param
* @param AbstractComposite|array $options
* @param bool $isRule
*/
protected function setMessages(NestedValidationException $e, $param, $options, $isRule)
{
$paramRules = $isRule ? $options->getRules() : $options['rules']->getRules();
// Get the names of all rules used for this param
$rulesNames = [];
foreach ($paramRules as $rule) {
$rulesNames[] = lcfirst((new ReflectionClass($rule))->getShortName());
}
$params = [
$e->findMessages($rulesNames)
];
// If default messages are defined
if (!empty($this->defaultMessages)) {
$params[] = $e->findMessages($this->defaultMessages);
}
// If global messages are defined
if (!empty($messages)) {
$params[] = $e->findMessages($messages);
}
// If individual messages are defined
if (!$isRule && isset($options['messages'])) {
$params[] = $e->findMessages($options['messages']);
}
$errors = array_filter(call_user_func_array('array_merge', $params));
$this->errors[$param] = $this->storeErrorsWithRules ? $errors : array_values($errors);
}
}