1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\JsonGuard\Constraints; |
4
|
|
|
|
5
|
|
|
use League\JsonGuard\ErrorCode; |
6
|
|
|
use League\JsonGuard\SubSchemaValidatorFactory; |
7
|
|
|
use League\JsonGuard\ValidationError; |
8
|
|
|
|
9
|
|
|
class AdditionalItems implements ParentSchemaAwareContainerInstanceConstraint |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
2 |
|
public static function validate( |
15
|
|
|
$data, |
16
|
|
|
$schema, |
17
|
|
|
$parameter, |
18
|
|
|
SubSchemaValidatorFactory $validatorFactory, |
19
|
|
|
$pointer = null |
20
|
|
|
) { |
21
|
2 |
|
if (!is_array($data) || $parameter === true) { |
22
|
2 |
|
return null; |
23
|
|
|
} |
24
|
|
|
|
25
|
2 |
|
if (!is_array($items = self::getItems($schema))) { |
26
|
2 |
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
if ($parameter === false) { |
30
|
2 |
|
return self::validateAdditionalItemsWhenNotAllowed($data, $items, $pointer); |
31
|
2 |
|
} elseif (is_object($parameter)) { |
32
|
2 |
|
$additionalItems = array_slice($data, count($items)); |
33
|
|
|
|
34
|
2 |
|
return self::validateAdditionalItemsAgainstSchema( |
35
|
2 |
|
$additionalItems, |
36
|
2 |
|
$parameter, |
37
|
2 |
|
$validatorFactory, |
38
|
|
|
$pointer |
39
|
2 |
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param object $schema |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
2 |
|
private static function getItems($schema) |
51
|
|
|
{ |
52
|
2 |
|
return property_exists($schema, 'items') ? $schema->items : null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $items |
57
|
|
|
* @param object $schema |
58
|
|
|
* @param \League\JsonGuard\SubSchemaValidatorFactory $validatorFactory |
59
|
|
|
* @param string $pointer |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
2 |
|
private static function validateAdditionalItemsAgainstSchema( |
64
|
|
|
$items, |
65
|
|
|
$schema, |
66
|
|
|
SubSchemaValidatorFactory $validatorFactory, |
67
|
|
|
$pointer |
68
|
|
|
) { |
69
|
2 |
|
$errors = []; |
70
|
2 |
|
foreach ($items as $key => $item) { |
71
|
2 |
|
$currentPointer = $pointer . '/' . $key; |
72
|
2 |
|
$validator = $validatorFactory->makeSubSchemaValidator($item, $schema, $currentPointer); |
73
|
2 |
|
$errors = array_merge($errors, $validator->errors()); |
74
|
2 |
|
} |
75
|
|
|
|
76
|
2 |
|
return $errors; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $data |
81
|
|
|
* @param array $items |
82
|
|
|
* @param $pointer |
83
|
|
|
* |
84
|
|
|
* @return \League\JsonGuard\ValidationError |
85
|
|
|
*/ |
86
|
2 |
|
private static function validateAdditionalItemsWhenNotAllowed($data, $items, $pointer) |
87
|
|
|
{ |
88
|
2 |
|
if (count($data) > count($items)) { |
89
|
2 |
|
return new ValidationError( |
90
|
2 |
|
'Additional items are not allowed.', |
91
|
2 |
|
ErrorCode::NOT_ALLOWED_ITEM, |
92
|
2 |
|
$data, |
93
|
|
|
$pointer |
94
|
2 |
|
); |
95
|
|
|
} |
96
|
2 |
|
} |
97
|
|
|
} |
98
|
|
|
|