|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Todd Burry <[email protected]> |
|
4
|
|
|
* @copyright 2009-2017 Vanilla Forums Inc. |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Garden\Schema; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A class for defining and validating data schemas. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Schema implements \JsonSerializable { |
|
14
|
|
|
/// Constants /// |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Throw a notice when extraneous properties are encountered during validation. |
|
18
|
|
|
*/ |
|
19
|
|
|
const FLAG_EXTRA_PROPERTIES_NOTICE = 0x1; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Throw a ValidationException when extraneous properties are encountered during validation. |
|
23
|
|
|
*/ |
|
24
|
|
|
const FLAG_EXTRA_PROPERTIES_EXCEPTION = 0x2; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array All the known types. |
|
28
|
|
|
* |
|
29
|
|
|
* If this is ever given some sort of public access then remove the static. |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $types = [ |
|
32
|
|
|
'a' => 'array', |
|
33
|
|
|
'o' => 'object', |
|
34
|
|
|
'i' => 'integer', |
|
35
|
|
|
'int' => 'integer', |
|
36
|
|
|
's' => 'string', |
|
37
|
|
|
'str' => 'string', |
|
38
|
|
|
'n' => 'number', |
|
39
|
|
|
'b' => 'boolean', |
|
40
|
|
|
'bool' => 'boolean', |
|
41
|
|
|
'ts' => 'timestamp', |
|
42
|
|
|
'dt' => 'datetime' |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
private $schema = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var int A bitwise combination of the various **Schema::FLAG_*** constants. |
|
49
|
|
|
*/ |
|
50
|
|
|
private $flags = 0; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array An array of callbacks that will custom validate the schema. |
|
54
|
|
|
*/ |
|
55
|
|
|
private $validators = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string|Validation The name of the class or an instance that will be cloned. |
|
59
|
|
|
*/ |
|
60
|
|
|
private $validationClass = Validation::class; |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/// Methods /// |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Initialize an instance of a new {@link Schema} class. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $schema The array schema to validate against. |
|
69
|
|
|
*/ |
|
70
|
128 |
|
public function __construct($schema = []) { |
|
71
|
128 |
|
$this->schema = $this->parse($schema); |
|
72
|
128 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Grab the schema's current description. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function getDescription() { |
|
80
|
1 |
|
return isset($this->schema['description']) ? $this->schema['description'] : ''; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set the description for the schema. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $description The new description. |
|
87
|
|
|
* @throws \InvalidArgumentException Throws an exception when the provided description is not a string. |
|
88
|
|
|
* @return Schema |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function setDescription($description) { |
|
91
|
2 |
|
if (is_string($description)) { |
|
92
|
1 |
|
$this->schema['description'] = $description; |
|
93
|
|
|
} else { |
|
94
|
1 |
|
throw new \InvalidArgumentException("The description is not a valid string.", 500); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Return the validation flags. |
|
102
|
|
|
* |
|
103
|
|
|
* @return int Returns a bitwise combination of flags. |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getFlags() { |
|
106
|
1 |
|
return $this->flags; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set the validation flags. |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $flags One or more of the **Schema::FLAG_*** constants. |
|
113
|
|
|
* @return Schema Returns the current instance for fluent calls. |
|
114
|
|
|
*/ |
|
115
|
8 |
|
public function setFlags($flags) { |
|
116
|
8 |
|
if (!is_int($flags)) { |
|
117
|
1 |
|
throw new \InvalidArgumentException('Invalid flags.', 500); |
|
118
|
|
|
} |
|
119
|
7 |
|
$this->flags = $flags; |
|
120
|
|
|
|
|
121
|
7 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Whether or not the schema has a flag (or combination of flags). |
|
126
|
|
|
* |
|
127
|
|
|
* @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
|
128
|
|
|
* @return bool Returns **true** if all of the flags are set or **false** otherwise. |
|
129
|
|
|
*/ |
|
130
|
8 |
|
public function hasFlag($flag) { |
|
131
|
8 |
|
return ($this->flags & $flag) === $flag; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set a flag. |
|
136
|
|
|
* |
|
137
|
|
|
* @param int $flag One or more of the **Schema::VALIDATE_*** constants. |
|
138
|
|
|
* @param bool $value Either true or false. |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function setFlag($flag, $value) { |
|
142
|
1 |
|
if ($value) { |
|
143
|
1 |
|
$this->flags = $this->flags | $flag; |
|
144
|
|
|
} else { |
|
145
|
1 |
|
$this->flags = $this->flags & ~$flag; |
|
146
|
|
|
} |
|
147
|
1 |
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Merge a schema with this one. |
|
152
|
|
|
* |
|
153
|
|
|
* @param Schema $schema A scheme instance. Its parameters will be merged into the current instance. |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function merge(Schema $schema) { |
|
156
|
|
|
$fn = function (array &$target, array $source) use (&$fn) { |
|
157
|
2 |
|
foreach ($source as $key => $val) { |
|
158
|
2 |
|
if (is_array($val) && array_key_exists($key, $target) && is_array($target[$key])) { |
|
159
|
2 |
|
if (isset($val[0]) || isset($target[$key][0])) { |
|
160
|
|
|
// This is a numeric array, so just do a merge. |
|
161
|
1 |
|
$merged = array_merge($target[$key], $val); |
|
162
|
1 |
|
if (is_string($merged[0])) { |
|
163
|
1 |
|
$merged = array_keys(array_flip($merged)); |
|
164
|
|
|
} |
|
165
|
1 |
|
$target[$key] = $merged; |
|
166
|
|
|
} else { |
|
167
|
2 |
|
$target[$key] = $fn($target[$key], $val); |
|
168
|
|
|
} |
|
169
|
|
|
} else { |
|
170
|
2 |
|
$target[$key] = $val; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
2 |
|
return $target; |
|
175
|
2 |
|
}; |
|
176
|
|
|
|
|
177
|
2 |
|
$fn($this->schema, $schema->jsonSerialize()); |
|
178
|
2 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Parse a schema in short form into a full schema array. |
|
182
|
|
|
* |
|
183
|
|
|
* @param array $arr The array to parse into a schema. |
|
184
|
|
|
* @return array The full schema array. |
|
185
|
|
|
* @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid. |
|
186
|
|
|
*/ |
|
187
|
128 |
|
public function parse(array $arr) { |
|
188
|
128 |
|
if (empty($arr)) { |
|
189
|
|
|
// An empty schema validates to anything. |
|
190
|
6 |
|
return []; |
|
191
|
123 |
|
} elseif (isset($arr['type'])) { |
|
192
|
|
|
// This is a long form schema and can be parsed as the root. |
|
193
|
2 |
|
return $this->parseNode($arr); |
|
194
|
|
|
} else { |
|
195
|
|
|
// Check for a root schema. |
|
196
|
123 |
|
$value = reset($arr); |
|
197
|
123 |
|
$key = key($arr); |
|
|
|
|
|
|
198
|
123 |
|
if (is_int($key)) { |
|
199
|
74 |
|
$key = $value; |
|
|
|
|
|
|
200
|
74 |
|
$value = null; |
|
201
|
|
|
} |
|
202
|
123 |
|
list ($name, $param) = $this->parseShortParam($key, $value); |
|
|
|
|
|
|
203
|
123 |
|
if (empty($name)) { |
|
204
|
42 |
|
return $this->parseNode($param, $value); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
// If we are here then this is n object schema. |
|
209
|
83 |
|
list($properties, $required) = $this->parseProperties($arr); |
|
210
|
|
|
|
|
211
|
|
|
$result = [ |
|
212
|
83 |
|
'type' => 'object', |
|
213
|
83 |
|
'properties' => $properties, |
|
214
|
83 |
|
'required' => $required |
|
215
|
|
|
]; |
|
216
|
|
|
|
|
217
|
83 |
|
return array_filter($result); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Parse a schema node. |
|
222
|
|
|
* |
|
223
|
|
|
* @param array $node The node to parse. |
|
224
|
|
|
* @param mixed $value Additional information from the node. |
|
225
|
|
|
* @return array Returns a JSON schema compatible node. |
|
226
|
|
|
*/ |
|
227
|
123 |
|
private function parseNode($node, $value = null) { |
|
228
|
123 |
|
if (is_array($value)) { |
|
229
|
|
|
// The value describes a bit more about the schema. |
|
230
|
47 |
|
switch ($node['type']) { |
|
231
|
47 |
|
case 'array': |
|
232
|
6 |
|
if (isset($value['items'])) { |
|
233
|
|
|
// The value includes array schema information. |
|
234
|
1 |
|
$node = array_replace($node, $value); |
|
235
|
|
|
} else { |
|
236
|
5 |
|
$node['items'] = $this->parse($value); |
|
237
|
|
|
} |
|
238
|
6 |
|
break; |
|
239
|
41 |
|
case 'object': |
|
240
|
|
|
// The value is a schema of the object. |
|
241
|
9 |
|
if (isset($value['properties'])) { |
|
242
|
|
|
list($node['properties']) = $this->parseProperties($value['properties']); |
|
243
|
|
|
} else { |
|
244
|
9 |
|
list($node['properties'], $required) = $this->parseProperties($value); |
|
245
|
9 |
|
if (!empty($required)) { |
|
246
|
9 |
|
$node['required'] = $required; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
9 |
|
break; |
|
250
|
|
|
default: |
|
251
|
32 |
|
$node = array_replace($node, $value); |
|
252
|
47 |
|
break; |
|
253
|
|
|
} |
|
254
|
92 |
|
} elseif (is_string($value)) { |
|
255
|
77 |
|
if ($node['type'] === 'array' && $arrType = $this->getType($value)) { |
|
256
|
2 |
|
$node['items'] = ['type' => $arrType]; |
|
257
|
|
|
} elseif (!empty($value)) { |
|
258
|
22 |
|
$node['description'] = $value; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
123 |
|
return $node; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Parse the schema for an object's properties. |
|
267
|
|
|
* |
|
268
|
|
|
* @param array $arr An object property schema. |
|
269
|
|
|
* @return array Returns a schema array suitable to be placed in the **properties** key of a schema. |
|
270
|
|
|
*/ |
|
271
|
83 |
|
private function parseProperties(array $arr) { |
|
272
|
83 |
|
$properties = []; |
|
|
|
|
|
|
273
|
83 |
|
$requiredProperties = []; |
|
274
|
83 |
|
foreach ($arr as $key => $value) { |
|
275
|
|
|
// Fix a schema specified as just a value. |
|
276
|
83 |
|
if (is_int($key)) { |
|
277
|
61 |
|
if (is_string($value)) { |
|
278
|
61 |
|
$key = $value; |
|
|
|
|
|
|
279
|
61 |
|
$value = ''; |
|
280
|
|
|
} else { |
|
281
|
|
|
throw new \InvalidArgumentException("Schema at position $key is not a valid parameter.", 500); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
// The parameter is defined in the key. |
|
286
|
83 |
|
list($name, $param, $required) = $this->parseShortParam($key, $value); |
|
287
|
|
|
|
|
288
|
83 |
|
$node = $this->parseNode($param, $value); |
|
289
|
|
|
|
|
290
|
83 |
|
$properties[$name] = $node; |
|
291
|
83 |
|
if ($required) { |
|
292
|
42 |
|
$requiredProperties[] = $name; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
83 |
|
return array($properties, $requiredProperties); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Parse a short parameter string into a full array parameter. |
|
300
|
|
|
* |
|
301
|
|
|
* @param string $key The short parameter string to parse. |
|
302
|
|
|
* @param array $value An array of other information that might help resolve ambiguity. |
|
303
|
|
|
* @return array Returns an array in the form `[string name, array param, bool required]`. |
|
304
|
|
|
* @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format. |
|
305
|
|
|
*/ |
|
306
|
123 |
|
public function parseShortParam($key, $value = []) { |
|
307
|
|
|
// Is the parameter optional? |
|
308
|
123 |
|
if (substr($key, -1) === '?') { |
|
309
|
59 |
|
$required = false; |
|
310
|
59 |
|
$key = substr($key, 0, -1); |
|
|
|
|
|
|
311
|
|
|
} else { |
|
312
|
82 |
|
$required = true; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
// Check for a type. |
|
316
|
123 |
|
$parts = explode(':', $key); |
|
317
|
123 |
|
$name = $parts[0]; |
|
|
|
|
|
|
318
|
123 |
|
$type = !empty($parts[1]) && isset(self::$types[$parts[1]]) ? self::$types[$parts[1]] : null; |
|
|
|
|
|
|
319
|
|
|
|
|
320
|
123 |
|
if ($value instanceof Schema) { |
|
321
|
2 |
|
if ($type === 'array') { |
|
322
|
1 |
|
$param = ['type' => $type, 'items' => $value]; |
|
323
|
|
|
} else { |
|
324
|
1 |
|
$param = $value; |
|
325
|
|
|
} |
|
326
|
123 |
|
} elseif (isset($value['type'])) { |
|
327
|
|
|
$param = $value; |
|
328
|
|
|
|
|
329
|
|
|
if (!empty($type) && $type !== $param['type']) { |
|
330
|
|
|
throw new \InvalidArgumentException("Type mismatch between $type and {$param['type']} for field $name.", 500); |
|
331
|
|
|
} |
|
332
|
|
|
} else { |
|
333
|
123 |
|
if (empty($type) && !empty($parts[1])) { |
|
334
|
|
|
throw new \InvalidArgumentException("Invalid type {$parts[1]} for field $name.", 500); |
|
335
|
|
|
} |
|
336
|
123 |
|
$param = ['type' => $type]; |
|
337
|
|
|
|
|
338
|
|
|
// Parsed required strings have a minimum length of 1. |
|
339
|
123 |
|
if ($type === 'string' && !empty($name) && $required) { |
|
340
|
27 |
|
$param['minLength'] = 1; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
123 |
|
return [$name, $param, $required]; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Add a custom validator to to validate the schema. |
|
349
|
|
|
* |
|
350
|
|
|
* @param string $fieldname The name of the field to validate, if any. |
|
351
|
|
|
* |
|
352
|
|
|
* If you are adding a validator to a deeply nested field then separate the path with dots. |
|
353
|
|
|
* @param callable $callback The callback to validate with. |
|
354
|
|
|
* @return Schema Returns `$this` for fluent calls. |
|
355
|
|
|
*/ |
|
356
|
2 |
|
public function addValidator($fieldname, callable $callback) { |
|
357
|
2 |
|
$this->validators[$fieldname][] = $callback; |
|
358
|
2 |
|
return $this; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Require one of a given set of fields in the schema. |
|
363
|
|
|
* |
|
364
|
|
|
* @param array $required The field names to require. |
|
365
|
|
|
* @param string $fieldname The name of the field to attach to. |
|
366
|
|
|
* @param int $count The count of required items. |
|
367
|
|
|
* @return Schema Returns `$this` for fluent calls. |
|
368
|
|
|
*/ |
|
369
|
1 |
|
public function requireOneOf(array $required, $fieldname = '', $count = 1) { |
|
370
|
1 |
|
$result = $this->addValidator( |
|
371
|
|
|
$fieldname, |
|
372
|
|
|
function ($data, ValidationField $field) use ($required, $count) { |
|
373
|
1 |
|
$hasCount = 0; |
|
|
|
|
|
|
374
|
1 |
|
$flattened = []; |
|
375
|
|
|
|
|
376
|
1 |
|
foreach ($required as $name) { |
|
377
|
1 |
|
$flattened = array_merge($flattened, (array)$name); |
|
378
|
|
|
|
|
379
|
1 |
|
if (is_array($name)) { |
|
380
|
|
|
// This is an array of required names. They all must match. |
|
381
|
1 |
|
$hasCountInner = 0; |
|
382
|
1 |
|
foreach ($name as $nameInner) { |
|
383
|
1 |
|
if (isset($data[$nameInner]) && $data[$nameInner]) { |
|
384
|
1 |
|
$hasCountInner++; |
|
385
|
|
|
} else { |
|
386
|
1 |
|
break; |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
1 |
|
if ($hasCountInner >= count($name)) { |
|
390
|
1 |
|
$hasCount++; |
|
391
|
|
|
} |
|
392
|
1 |
|
} elseif (isset($data[$name]) && $data[$name]) { |
|
393
|
1 |
|
$hasCount++; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
1 |
|
if ($hasCount >= $count) { |
|
397
|
1 |
|
return true; |
|
398
|
|
|
} |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
1 |
|
if ($count === 1) { |
|
402
|
1 |
|
$message = 'One of {required} are required.'; |
|
403
|
|
|
} else { |
|
404
|
|
|
$message = '{count} of {required} are required.'; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
1 |
|
$field->addError('missingField', [ |
|
408
|
1 |
|
'messageCode' => $message, |
|
409
|
1 |
|
'required' => $required, |
|
410
|
1 |
|
'count' => $count |
|
411
|
|
|
]); |
|
412
|
1 |
|
return false; |
|
413
|
1 |
|
} |
|
414
|
|
|
); |
|
415
|
|
|
|
|
416
|
1 |
|
return $result; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* Validate data against the schema. |
|
421
|
|
|
* |
|
422
|
|
|
* @param mixed $data The data to validate. |
|
423
|
|
|
* @param bool $sparse Whether or not this is a sparse validation. |
|
424
|
|
|
* @return mixed Returns a cleaned version of the data. |
|
425
|
|
|
* @throws ValidationException Throws an exception when the data does not validate against the schema. |
|
426
|
|
|
*/ |
|
427
|
106 |
|
public function validate($data, $sparse = false) { |
|
428
|
106 |
|
$validation = new ValidationField($this->createValidation(), $this->schema, ''); |
|
429
|
|
|
|
|
430
|
106 |
|
$clean = $this->validateField($data, $validation, $sparse); |
|
431
|
|
|
|
|
432
|
104 |
|
if (!$validation->getValidation()->isValid()) { |
|
433
|
55 |
|
throw new ValidationException($validation->getValidation()); |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
60 |
|
return $clean; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* Validate data against the schema and return the result. |
|
441
|
|
|
* |
|
442
|
|
|
* @param mixed $data The data to validate. |
|
443
|
|
|
* @param bool $sparse Whether or not to do a sparse validation. |
|
444
|
|
|
* @return bool Returns true if the data is valid. False otherwise. |
|
445
|
|
|
*/ |
|
446
|
33 |
|
public function isValid($data, $sparse = false) { |
|
447
|
|
|
try { |
|
448
|
33 |
|
$this->validate($data, $sparse); |
|
449
|
23 |
|
return true; |
|
450
|
16 |
|
} catch (ValidationException $ex) { |
|
451
|
16 |
|
return false; |
|
452
|
|
|
} |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
/** |
|
456
|
|
|
* Validate a field. |
|
457
|
|
|
* |
|
458
|
|
|
* @param mixed $value The value to validate. |
|
459
|
|
|
* @param ValidationField $field A validation object to add errors to. |
|
460
|
|
|
* @param bool $sparse Whether or not this is a sparse validation. |
|
461
|
|
|
* @return mixed Returns a clean version of the value with all extra fields stripped out. |
|
462
|
|
|
*/ |
|
463
|
106 |
|
private function validateField($value, ValidationField $field, $sparse = false) { |
|
464
|
106 |
|
$result = $value; |
|
465
|
106 |
|
if ($field->getField() instanceof Schema) { |
|
466
|
|
|
try { |
|
467
|
1 |
|
$result = $field->getField()->validate($value, $sparse); |
|
468
|
1 |
|
} catch (ValidationException $ex) { |
|
469
|
|
|
// The validation failed, so merge the validations together. |
|
470
|
1 |
|
$field->getValidation()->merge($ex->getValidation(), $field->getName()); |
|
471
|
|
|
} |
|
472
|
|
|
} else { |
|
473
|
|
|
// Validate the field's type. |
|
474
|
106 |
|
$type = $field->getType(); |
|
475
|
|
|
switch ($type) { |
|
476
|
106 |
|
case 'boolean': |
|
477
|
19 |
|
$result = $this->validateBoolean($value, $field); |
|
478
|
19 |
|
break; |
|
479
|
94 |
|
case 'integer': |
|
480
|
20 |
|
$result = $this->validateInteger($value, $field); |
|
481
|
20 |
|
break; |
|
482
|
93 |
|
case 'number': |
|
483
|
7 |
|
$result = $this->validateNumber($value, $field); |
|
484
|
7 |
|
break; |
|
485
|
93 |
|
case 'string': |
|
486
|
49 |
|
$result = $this->validateString($value, $field); |
|
487
|
49 |
|
break; |
|
488
|
77 |
|
case 'timestamp': |
|
489
|
6 |
|
$result = $this->validateTimestamp($value, $field); |
|
490
|
6 |
|
break; |
|
491
|
77 |
|
case 'datetime': |
|
492
|
7 |
|
$result = $this->validateDatetime($value, $field); |
|
493
|
7 |
|
break; |
|
494
|
75 |
|
case 'array': |
|
495
|
10 |
|
$result = $this->validateArray($value, $field, $sparse); |
|
496
|
10 |
|
break; |
|
497
|
74 |
|
case 'object': |
|
498
|
73 |
|
$result = $this->validateObject($value, $field, $sparse); |
|
499
|
71 |
|
break; |
|
500
|
2 |
|
case null: |
|
|
|
|
|
|
501
|
|
|
// No type was specified so we are valid. |
|
502
|
2 |
|
$result = $value; |
|
503
|
2 |
|
break; |
|
504
|
|
|
default: |
|
505
|
|
|
throw new \InvalidArgumentException("Unrecognized type $type.", 500); |
|
506
|
|
|
} |
|
507
|
106 |
|
if ($result !== null && !$this->validateEnum($value, $field)) { |
|
508
|
1 |
|
$result = null; |
|
509
|
|
|
} |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
// Validate a custom field validator. |
|
513
|
106 |
|
if ($result !== null) { |
|
514
|
105 |
|
$this->callValidators($result, $field); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
106 |
|
return $result; |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* Call all of the validators attached to a field. |
|
522
|
|
|
* |
|
523
|
|
|
* @param mixed $value The field value being validated. |
|
524
|
|
|
* @param ValidationField $field The validation object to add errors. |
|
525
|
|
|
*/ |
|
526
|
105 |
|
private function callValidators($value, ValidationField $field) { |
|
527
|
|
|
// Strip array references in the name except for the last one. |
|
528
|
105 |
|
$key = preg_replace(['`\[\d+\]$`', '`\[\d+\]`'], ['[]', ''], $field->getName()); |
|
529
|
105 |
|
if (!empty($this->validators[$key])) { |
|
530
|
2 |
|
foreach ($this->validators[$key] as $validator) { |
|
531
|
2 |
|
call_user_func($validator, $value, $field); |
|
532
|
|
|
} |
|
533
|
|
|
} |
|
534
|
105 |
|
} |
|
535
|
|
|
|
|
536
|
|
|
/** |
|
537
|
|
|
* Validate an array. |
|
538
|
|
|
* |
|
539
|
|
|
* @param mixed $value The value to validate. |
|
540
|
|
|
* @param ValidationField $field The validation results to add. |
|
541
|
|
|
* @param bool $sparse Whether or not this is a sparse validation. |
|
542
|
|
|
* @return array|null Returns an array or **null** if validation fails. |
|
543
|
|
|
*/ |
|
544
|
10 |
|
private function validateArray($value, ValidationField $field, $sparse = false) { |
|
545
|
10 |
|
if (!is_array($value) || (count($value) > 0 && !array_key_exists(0, $value))) { |
|
546
|
6 |
|
$field->addTypeError('array'); |
|
547
|
6 |
|
return null; |
|
548
|
|
|
} elseif (empty($value)) { |
|
549
|
1 |
|
return []; |
|
550
|
5 |
|
} elseif ($field->val('items') !== null) { |
|
551
|
4 |
|
$result = []; |
|
552
|
|
|
|
|
553
|
|
|
// Validate each of the types. |
|
554
|
4 |
|
$itemValidation = new ValidationField( |
|
555
|
4 |
|
$field->getValidation(), |
|
556
|
4 |
|
$field->val('items'), |
|
557
|
4 |
|
'' |
|
558
|
|
|
); |
|
559
|
|
|
|
|
560
|
4 |
|
foreach ($value as $i => &$item) { |
|
561
|
4 |
|
$itemValidation->setName($field->getName()."[{$i}]"); |
|
562
|
4 |
|
$validItem = $this->validateField($item, $itemValidation, $sparse); |
|
563
|
4 |
|
if ($validItem !== null) { |
|
564
|
4 |
|
$result[] = $validItem; |
|
565
|
|
|
} |
|
566
|
|
|
} |
|
567
|
|
|
} else { |
|
568
|
|
|
// Cast the items into a proper numeric array. |
|
569
|
1 |
|
$result = array_values($value); |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
5 |
|
return $result; |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
/** |
|
576
|
|
|
* Validate a boolean value. |
|
577
|
|
|
* |
|
578
|
|
|
* @param mixed $value The value to validate. |
|
579
|
|
|
* @param ValidationField $field The validation results to add. |
|
580
|
|
|
* @return bool|null Returns the cleaned value or **null** if validation fails. |
|
581
|
|
|
*/ |
|
582
|
19 |
|
private function validateBoolean($value, ValidationField $field) { |
|
583
|
19 |
|
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
|
584
|
19 |
|
if ($value === null) { |
|
585
|
3 |
|
$field->addTypeError('boolean'); |
|
586
|
|
|
} |
|
587
|
19 |
|
return $value; |
|
588
|
|
|
} |
|
589
|
|
|
|
|
590
|
|
|
/** |
|
591
|
|
|
* Validate a date time. |
|
592
|
|
|
* |
|
593
|
|
|
* @param mixed $value The value to validate. |
|
594
|
|
|
* @param ValidationField $field The validation results to add. |
|
595
|
|
|
* @return \DateTimeInterface|null Returns the cleaned value or **null** if it isn't valid. |
|
596
|
|
|
*/ |
|
597
|
11 |
|
private function validateDatetime($value, ValidationField $field) { |
|
598
|
11 |
|
if ($value instanceof \DateTimeInterface) { |
|
|
|
|
|
|
599
|
|
|
// do nothing, we're good |
|
600
|
10 |
|
} elseif (is_string($value) && $value !== '') { |
|
601
|
|
|
try { |
|
602
|
7 |
|
$dt = new \DateTimeImmutable($value); |
|
603
|
5 |
|
if ($dt) { |
|
604
|
5 |
|
$value = $dt; |
|
605
|
|
|
} else { |
|
606
|
|
|
$value = null; |
|
607
|
|
|
} |
|
608
|
2 |
|
} catch (\Exception $ex) { |
|
609
|
2 |
|
$value = null; |
|
610
|
|
|
} |
|
611
|
3 |
|
} elseif (is_int($value) && $value > 0) { |
|
612
|
1 |
|
$value = new \DateTimeImmutable('@'.(string)round($value)); |
|
613
|
|
|
} else { |
|
614
|
2 |
|
$value = null; |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
11 |
|
if ($value === null) { |
|
618
|
4 |
|
$field->addTypeError('datetime'); |
|
619
|
|
|
} |
|
620
|
11 |
|
return $value; |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* Validate a float. |
|
625
|
|
|
* |
|
626
|
|
|
* @param mixed $value The value to validate. |
|
627
|
|
|
* @param ValidationField $field The validation results to add. |
|
628
|
|
|
* @return float|int|null Returns a number or **null** if validation fails. |
|
629
|
|
|
*/ |
|
630
|
7 |
|
private function validateNumber($value, ValidationField $field) { |
|
631
|
7 |
|
if (is_float($value) || is_int($value)) { |
|
632
|
2 |
|
$result = $value; |
|
633
|
5 |
|
} elseif (is_numeric($value)) { |
|
634
|
1 |
|
$result = (float)$value; |
|
635
|
|
|
} else { |
|
636
|
4 |
|
$result = null; |
|
637
|
4 |
|
$field->addTypeError('number'); |
|
638
|
|
|
} |
|
639
|
7 |
|
return $result; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
/** |
|
643
|
|
|
* Validate and integer. |
|
644
|
|
|
* |
|
645
|
|
|
* @param mixed $value The value to validate. |
|
646
|
|
|
* @param ValidationField $field The validation results to add. |
|
647
|
|
|
* @return int|null Returns the cleaned value or **null** if validation fails. |
|
648
|
|
|
*/ |
|
649
|
20 |
|
private function validateInteger($value, ValidationField $field) { |
|
650
|
20 |
|
if (is_int($value)) { |
|
|
|
|
|
|
651
|
|
|
// Do nothing, we're good. |
|
652
|
9 |
|
} elseif (is_numeric($value)) { |
|
653
|
2 |
|
$value = (int)$value; |
|
654
|
|
|
} else { |
|
655
|
8 |
|
$value = null; |
|
656
|
8 |
|
$field->addTypeError('integer'); |
|
657
|
|
|
} |
|
658
|
20 |
|
return $value; |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
/** |
|
662
|
|
|
* Validate an object. |
|
663
|
|
|
* |
|
664
|
|
|
* @param mixed $value The value to validate. |
|
665
|
|
|
* @param ValidationField $field The validation results to add. |
|
666
|
|
|
* @param bool $sparse Whether or not this is a sparse validation. |
|
667
|
|
|
* @return object|null Returns a clean object or **null** if validation fails. |
|
|
|
|
|
|
668
|
|
|
*/ |
|
669
|
73 |
|
private function validateObject($value, ValidationField $field, $sparse = false) { |
|
670
|
73 |
|
if (!is_array($value) || isset($value[0])) { |
|
671
|
6 |
|
$field->addTypeError('object'); |
|
672
|
6 |
|
return null; |
|
673
|
73 |
|
} elseif (is_array($field->val('properties'))) { |
|
674
|
|
|
// Validate the data against the internal schema. |
|
675
|
73 |
|
$value = $this->validateProperties($value, $field, $sparse); |
|
676
|
|
|
} |
|
677
|
71 |
|
return $value; |
|
678
|
|
|
} |
|
679
|
|
|
|
|
680
|
|
|
/** |
|
681
|
|
|
* Validate data against the schema and return the result. |
|
682
|
|
|
* |
|
683
|
|
|
* @param array $data The data to validate. |
|
684
|
|
|
* @param ValidationField $field This argument will be filled with the validation result. |
|
685
|
|
|
* @param bool $sparse Whether or not this is a sparse validation. |
|
686
|
|
|
* @return array Returns a clean array with only the appropriate properties and the data coerced to proper types. |
|
687
|
|
|
*/ |
|
688
|
73 |
|
private function validateProperties(array $data, ValidationField $field, $sparse = false) { |
|
689
|
73 |
|
$properties = $field->val('properties', []); |
|
690
|
73 |
|
$required = array_flip($field->val('required', [])); |
|
|
|
|
|
|
691
|
73 |
|
$keys = array_keys($data); |
|
|
|
|
|
|
692
|
73 |
|
$keys = array_combine(array_map('strtolower', $keys), $keys); |
|
|
|
|
|
|
693
|
|
|
|
|
694
|
73 |
|
$propertyField = new ValidationField($field->getValidation(), [], null); |
|
695
|
|
|
|
|
696
|
|
|
// Loop through the schema fields and validate each one. |
|
697
|
73 |
|
$clean = []; |
|
698
|
73 |
|
foreach ($properties as $propertyName => $property) { |
|
699
|
|
|
$propertyField |
|
700
|
73 |
|
->setField($property) |
|
701
|
73 |
|
->setName(ltrim($field->getName().".$propertyName", '.')); |
|
702
|
|
|
|
|
703
|
73 |
|
$lName = strtolower($propertyName); |
|
|
|
|
|
|
704
|
73 |
|
$isRequired = isset($required[$propertyName]); |
|
705
|
|
|
|
|
706
|
|
|
// First check for required fields. |
|
707
|
73 |
|
if (!array_key_exists($lName, $keys)) { |
|
708
|
|
|
// A sparse validation can leave required fields out. |
|
709
|
18 |
|
if ($isRequired && !$sparse) { |
|
710
|
6 |
|
$propertyField->addError('missingField', ['messageCode' => '{field} is required.']); |
|
711
|
|
|
} |
|
712
|
73 |
|
} elseif ($data[$keys[$lName]] === null) { |
|
713
|
17 |
|
if ($isRequired) { |
|
714
|
9 |
|
$propertyField->addError('missingField', ['messageCode' => '{field} cannot be null.']); |
|
715
|
|
|
} else { |
|
716
|
8 |
|
$clean[$propertyName] = null; |
|
717
|
|
|
} |
|
718
|
|
|
} else { |
|
719
|
65 |
|
$clean[$propertyName] = $this->validateField($data[$keys[$lName]], $propertyField, $sparse); |
|
720
|
|
|
} |
|
721
|
|
|
|
|
722
|
73 |
|
unset($keys[$lName]); |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
|
|
// Look for extraneous properties. |
|
726
|
73 |
|
if (!empty($keys)) { |
|
727
|
7 |
|
if ($this->hasFlag(Schema::FLAG_EXTRA_PROPERTIES_NOTICE)) { |
|
|
|
|
|
|
728
|
2 |
|
$msg = sprintf("%s has unexpected field(s): %s.", $field->getName() ?: 'value', implode(', ', $keys)); |
|
729
|
2 |
|
trigger_error($msg, E_USER_NOTICE); |
|
730
|
|
|
} |
|
731
|
|
|
|
|
732
|
5 |
|
if ($this->hasFlag(Schema::FLAG_EXTRA_PROPERTIES_EXCEPTION)) { |
|
|
|
|
|
|
733
|
2 |
|
$field->addError('invalid', [ |
|
734
|
2 |
|
'messageCode' => '{field} has {extra,plural,an unexpected field,unexpected fields}: {extra}.', |
|
735
|
2 |
|
'extra' => array_values($keys), |
|
736
|
2 |
|
'status' => 422 |
|
737
|
|
|
]); |
|
738
|
|
|
} |
|
739
|
|
|
} |
|
740
|
|
|
|
|
741
|
71 |
|
return $clean; |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
|
|
/** |
|
745
|
|
|
* Validate a string. |
|
746
|
|
|
* |
|
747
|
|
|
* @param mixed $value The value to validate. |
|
748
|
|
|
* @param ValidationField $field The validation results to add. |
|
749
|
|
|
* @return string|null Returns the valid string or **null** if validation fails. |
|
750
|
|
|
*/ |
|
751
|
49 |
|
private function validateString($value, ValidationField $field) { |
|
752
|
49 |
|
if (is_string($value) || is_numeric($value)) { |
|
753
|
47 |
|
$value = $result = (string)$value; |
|
754
|
|
|
} else { |
|
755
|
3 |
|
$field->addTypeError('string'); |
|
756
|
3 |
|
return null; |
|
757
|
|
|
} |
|
758
|
|
|
|
|
759
|
47 |
|
if (($minLength = $field->val('minLength', 0)) > 0 && mb_strlen($value) < $minLength) { |
|
760
|
4 |
|
if (!empty($field->getName()) && $minLength === 1) { |
|
761
|
2 |
|
$field->addError('missingField', ['messageCode' => '{field} is required.', 'status' => 422]); |
|
762
|
|
|
} else { |
|
763
|
2 |
|
$field->addError( |
|
764
|
2 |
|
'minLength', |
|
765
|
|
|
[ |
|
766
|
2 |
|
'messageCode' => '{field} should be at least {minLength} {minLength,plural,character} long.', |
|
767
|
2 |
|
'minLength' => $minLength, |
|
768
|
2 |
|
'status' => 422 |
|
769
|
|
|
] |
|
770
|
|
|
); |
|
771
|
|
|
} |
|
772
|
4 |
|
$result = null; |
|
773
|
|
|
} |
|
774
|
47 |
|
if (($maxLength = $field->val('maxLength', 0)) > 0 && mb_strlen($value) > $maxLength) { |
|
775
|
1 |
|
$field->addError( |
|
776
|
1 |
|
'maxLength', |
|
777
|
|
|
[ |
|
778
|
1 |
|
'messageCode' => '{field} is {overflow} {overflow,plural,characters} too long.', |
|
779
|
1 |
|
'maxLength' => $maxLength, |
|
780
|
1 |
|
'overflow' => mb_strlen($value) - $maxLength, |
|
781
|
1 |
|
'status' => 422 |
|
782
|
|
|
] |
|
783
|
|
|
); |
|
784
|
1 |
|
$result = null; |
|
785
|
|
|
} |
|
786
|
47 |
|
if ($pattern = $field->val('pattern')) { |
|
787
|
4 |
|
$regex = '`'.str_replace('`', preg_quote('`', '`'), $pattern).'`'; |
|
788
|
|
|
|
|
789
|
4 |
|
if (!preg_match($regex, $value)) { |
|
790
|
2 |
|
$field->addError( |
|
791
|
2 |
|
'invalid', |
|
792
|
|
|
[ |
|
793
|
2 |
|
'messageCode' => '{field} is in the incorrect format.', |
|
794
|
|
|
'status' => 422 |
|
795
|
|
|
] |
|
796
|
|
|
); |
|
797
|
|
|
} |
|
798
|
4 |
|
$result = null; |
|
799
|
|
|
} |
|
800
|
47 |
|
if ($format = $field->val('format')) { |
|
801
|
15 |
|
$type = $format; |
|
802
|
|
|
switch ($format) { |
|
803
|
15 |
|
case 'date-time': |
|
804
|
4 |
|
$result = $this->validateDatetime($result, $field); |
|
805
|
4 |
|
if ($result instanceof \DateTimeInterface) { |
|
806
|
4 |
|
$result = $result->format(\DateTime::RFC3339); |
|
807
|
|
|
} |
|
808
|
4 |
|
break; |
|
809
|
11 |
|
case 'email': |
|
810
|
1 |
|
$result = filter_var($result, FILTER_VALIDATE_EMAIL); |
|
811
|
1 |
|
break; |
|
812
|
10 |
|
case 'ipv4': |
|
813
|
1 |
|
$type = 'IPv4 address'; |
|
|
|
|
|
|
814
|
1 |
|
$result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
|
815
|
1 |
|
break; |
|
816
|
9 |
|
case 'ipv6': |
|
817
|
1 |
|
$type = 'IPv6 address'; |
|
|
|
|
|
|
818
|
1 |
|
$result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); |
|
819
|
1 |
|
break; |
|
820
|
8 |
|
case 'ip': |
|
821
|
1 |
|
$type = 'IP address'; |
|
|
|
|
|
|
822
|
1 |
|
$result = filter_var($result, FILTER_VALIDATE_IP); |
|
823
|
1 |
|
break; |
|
824
|
7 |
|
case 'uri': |
|
825
|
7 |
|
$type = 'URI'; |
|
|
|
|
|
|
826
|
7 |
|
$result = filter_var($result, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_SCHEME_REQUIRED); |
|
827
|
7 |
|
break; |
|
828
|
|
|
default: |
|
829
|
|
|
trigger_error("Unrecognized format '$format'.", E_USER_NOTICE); |
|
830
|
|
|
} |
|
831
|
15 |
|
if ($result === false) { |
|
832
|
5 |
|
$field->addTypeError($type); |
|
833
|
|
|
} |
|
834
|
|
|
} |
|
835
|
|
|
|
|
836
|
47 |
|
return $result; |
|
837
|
|
|
} |
|
838
|
|
|
|
|
839
|
|
|
/** |
|
840
|
|
|
* Validate a unix timestamp. |
|
841
|
|
|
* |
|
842
|
|
|
* @param mixed $value The value to validate. |
|
843
|
|
|
* @param ValidationField $field The field being validated. |
|
844
|
|
|
* @return int|null Returns a valid timestamp or **null** if the value doesn't validate. |
|
845
|
|
|
*/ |
|
846
|
6 |
|
private function validateTimestamp($value, ValidationField $field) { |
|
847
|
6 |
|
if (is_numeric($value) && $value > 0) { |
|
848
|
1 |
|
$result = (int)$value; |
|
849
|
5 |
|
} elseif (is_string($value) && $ts = strtotime($value)) { |
|
850
|
1 |
|
$result = $ts; |
|
851
|
|
|
} else { |
|
852
|
4 |
|
$field->addTypeError('timestamp'); |
|
853
|
4 |
|
$result = null; |
|
854
|
|
|
} |
|
855
|
6 |
|
return $result; |
|
856
|
|
|
} |
|
857
|
|
|
|
|
858
|
|
|
/** |
|
859
|
|
|
* Validate a value against an enum. |
|
860
|
|
|
* |
|
861
|
|
|
* @param mixed $value The value to test. |
|
862
|
|
|
* @param ValidationField $field The validation object for adding errors. |
|
863
|
|
|
* @return bool Returns **true** if the value one of the enumerated values or **false** otherwise. |
|
864
|
|
|
*/ |
|
865
|
105 |
|
private function validateEnum($value, ValidationField $field) { |
|
866
|
105 |
|
$enum = $field->val('enum'); |
|
867
|
105 |
|
if (empty($enum)) { |
|
868
|
104 |
|
return true; |
|
869
|
|
|
} |
|
870
|
|
|
|
|
871
|
1 |
|
if (!in_array($value, $enum, true)) { |
|
872
|
1 |
|
$field->addError( |
|
873
|
1 |
|
'invalid', |
|
874
|
|
|
[ |
|
875
|
1 |
|
'messageCode' => '{field} must be one of: {enum}.', |
|
876
|
1 |
|
'enum' => $enum, |
|
877
|
1 |
|
'status' => 422 |
|
878
|
|
|
] |
|
879
|
|
|
); |
|
880
|
1 |
|
return false; |
|
881
|
|
|
} |
|
882
|
1 |
|
return true; |
|
883
|
|
|
} |
|
884
|
|
|
|
|
885
|
|
|
/** |
|
886
|
|
|
* Specify data which should be serialized to JSON. |
|
887
|
|
|
* |
|
888
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
889
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
890
|
|
|
* which is a value of any type other than a resource. |
|
891
|
|
|
*/ |
|
892
|
20 |
|
public function jsonSerialize() { |
|
893
|
20 |
|
$result = $this->schema; |
|
894
|
20 |
|
array_walk_recursive($result, function (&$value) { |
|
895
|
20 |
|
if ($value instanceof \JsonSerializable) { |
|
896
|
1 |
|
$value = $value->jsonSerialize(); |
|
897
|
|
|
} |
|
898
|
20 |
|
}); |
|
899
|
20 |
|
return $result; |
|
900
|
|
|
} |
|
901
|
|
|
|
|
902
|
|
|
/** |
|
903
|
|
|
* Look up a type based on its alias. |
|
904
|
|
|
* |
|
905
|
|
|
* @param string $alias The type alias or type name to lookup. |
|
906
|
|
|
* @return mixed |
|
907
|
|
|
*/ |
|
908
|
9 |
|
private function getType($alias) { |
|
909
|
9 |
|
if (isset(self::$types[$alias])) { |
|
910
|
2 |
|
$type = self::$types[$alias]; |
|
911
|
8 |
|
} elseif (array_search($alias, self::$types) !== false) { |
|
912
|
|
|
$type = $alias; |
|
913
|
|
|
} else { |
|
914
|
8 |
|
$type = null; |
|
915
|
|
|
} |
|
916
|
9 |
|
return $type; |
|
917
|
|
|
} |
|
918
|
|
|
|
|
919
|
|
|
/** |
|
920
|
|
|
* Get the class that's used to contain validation information. |
|
921
|
|
|
* |
|
922
|
|
|
* @return Validation|string Returns the validation class. |
|
923
|
|
|
*/ |
|
924
|
106 |
|
public function getValidationClass() { |
|
925
|
106 |
|
return $this->validationClass; |
|
926
|
|
|
} |
|
927
|
|
|
|
|
928
|
|
|
/** |
|
929
|
|
|
* Set the class that's used to contain validation information. |
|
930
|
|
|
* |
|
931
|
|
|
* @param Validation|string $class Either the name of a class or a class that will be cloned. |
|
932
|
|
|
* @return $this |
|
933
|
|
|
*/ |
|
934
|
1 |
|
public function setValidationClass($class) { |
|
935
|
1 |
|
if (!is_a($class, Validation::class, true)) { |
|
936
|
|
|
throw new \InvalidArgumentException("$class must be a subclass of ".Validation::class, 500); |
|
937
|
|
|
} |
|
938
|
|
|
|
|
939
|
1 |
|
$this->validationClass = $class; |
|
940
|
1 |
|
return $this; |
|
941
|
|
|
} |
|
942
|
|
|
|
|
943
|
|
|
/** |
|
944
|
|
|
* Create a new validation instance. |
|
945
|
|
|
* |
|
946
|
|
|
* @return Validation Returns a validation object. |
|
947
|
|
|
*/ |
|
948
|
106 |
|
protected function createValidation() { |
|
949
|
106 |
|
$class = $this->getValidationClass(); |
|
950
|
|
|
|
|
951
|
106 |
|
if ($class instanceof Validation) { |
|
952
|
1 |
|
$result = clone $class; |
|
953
|
|
|
} else { |
|
954
|
106 |
|
$result = new $class; |
|
955
|
|
|
} |
|
956
|
106 |
|
return $result; |
|
957
|
|
|
} |
|
958
|
|
|
} |
|
959
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.