1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the JVal package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace JVal; |
11
|
|
|
|
12
|
|
|
use stdClass; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implements the three steps needed to perform a JSON Schema validation, |
16
|
|
|
* i.e. distinct methods to recursively:. |
17
|
|
|
* |
18
|
|
|
* 1) resolve JSON pointer references within schema |
19
|
|
|
* 2) normalize and validate the syntax of the schema |
20
|
|
|
* 3) validate a given instance against it |
21
|
|
|
*/ |
22
|
|
|
class Walker |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Registry |
26
|
|
|
*/ |
27
|
|
|
private $registry; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Resolver |
31
|
|
|
*/ |
32
|
|
|
private $resolver; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var stdClass[] |
36
|
|
|
*/ |
37
|
|
|
private $parsedSchemas = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var stdClass[] |
41
|
|
|
*/ |
42
|
|
|
private $resolvedSchemas = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Constraint[][] |
46
|
|
|
*/ |
47
|
|
|
private $constraintsCacheA = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Constraint[][] |
51
|
|
|
*/ |
52
|
|
|
private $constraintsCacheB = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor. |
56
|
|
|
* |
57
|
|
|
* @param Registry $registry |
58
|
|
|
* @param Resolver $resolver |
59
|
|
|
*/ |
60
|
485 |
|
public function __construct(Registry $registry, Resolver $resolver) |
61
|
|
|
{ |
62
|
485 |
|
$this->registry = $registry; |
63
|
485 |
|
$this->resolver = $resolver; |
64
|
485 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Recursively resolve JSON pointer references within a given schema. |
68
|
|
|
* |
69
|
|
|
* @param stdClass $schema The schema to resolve |
70
|
|
|
* @param Uri $uri The URI of the schema |
71
|
|
|
* |
72
|
|
|
* @return stdClass |
73
|
|
|
*/ |
74
|
299 |
|
public function resolveReferences(stdClass $schema, Uri $uri) |
75
|
|
|
{ |
76
|
299 |
|
if ($this->isLooping($schema, $this->resolvedSchemas)) { |
77
|
20 |
|
return $schema; |
78
|
|
|
} |
79
|
|
|
|
80
|
299 |
|
if (!$this->resolver->hasBaseSchema()) { |
81
|
299 |
|
$this->resolver->setBaseSchema($schema, $uri); |
82
|
299 |
|
} |
83
|
|
|
|
84
|
299 |
|
$inScope = false; |
85
|
|
|
|
86
|
299 |
|
if (property_exists($schema, 'id') && is_string($schema->id)) { |
87
|
7 |
|
$this->resolver->enter(new Uri($schema->id)); |
88
|
7 |
|
$inScope = true; |
89
|
7 |
|
} |
90
|
|
|
|
91
|
299 |
|
if (property_exists($schema, '$ref')) { |
92
|
22 |
|
$resolved = $this->resolver->resolve($schema); |
93
|
22 |
|
$this->resolver->enter($resolved[0], $resolved[1]); |
94
|
22 |
|
$schema = $this->resolveReferences($resolved[1], $resolved[0]); |
95
|
22 |
|
$this->resolver->leave(); |
96
|
22 |
|
} else { |
97
|
299 |
|
foreach ($schema as $property => $value) { |
98
|
299 |
|
if (is_object($value)) { |
99
|
114 |
|
$schema->{$property} = $this->resolveReferences($value, $uri); |
100
|
299 |
|
} elseif (is_array($value)) { |
101
|
75 |
|
foreach ($value as $index => $element) { |
102
|
73 |
|
if (is_object($element)) { |
103
|
44 |
|
$schema->{$property}[$index] = $this->resolveReferences($element, $uri); |
104
|
44 |
|
} |
105
|
75 |
|
} |
106
|
75 |
|
} |
107
|
299 |
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
299 |
|
if ($inScope) { |
111
|
7 |
|
$this->resolver->leave(); |
112
|
7 |
|
} |
113
|
|
|
|
114
|
299 |
|
return $schema; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Recursively normalizes a given schema and validates its syntax. |
119
|
|
|
* |
120
|
|
|
* @param stdClass $schema |
121
|
|
|
* @param Context $context |
122
|
|
|
* |
123
|
|
|
* @return stdClass |
124
|
|
|
*/ |
125
|
357 |
|
public function parseSchema(stdClass $schema, Context $context) |
126
|
|
|
{ |
127
|
357 |
|
if ($this->isLooping($schema, $this->parsedSchemas)) { |
128
|
12 |
|
return $schema; |
129
|
|
|
} |
130
|
|
|
|
131
|
357 |
|
foreach ($this->getConstraints($schema, $context) as $constraint) { |
132
|
357 |
|
foreach ($constraint->keywords() as $keyword) { |
133
|
357 |
|
if (property_exists($schema, $keyword)) { |
134
|
333 |
|
$constraint->normalize($schema, $context, $this); |
135
|
333 |
|
break; |
136
|
|
|
} |
137
|
357 |
|
} |
138
|
357 |
|
} |
139
|
|
|
|
140
|
357 |
|
return $schema; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Validates an instance against a given schema, populating a context |
145
|
|
|
* with encountered violations. |
146
|
|
|
* |
147
|
|
|
* @param mixed $instance |
148
|
|
|
* @param stdClass $schema |
149
|
|
|
* @param Context $context |
150
|
|
|
*/ |
151
|
348 |
|
public function applyConstraints($instance, stdClass $schema, Context $context) |
152
|
|
|
{ |
153
|
348 |
|
$constraints = $this->getConstraintsForInstance($schema, $context, $instance); |
154
|
348 |
|
foreach ($constraints as $constraint) { |
155
|
307 |
|
$constraint->apply($instance, $schema, $context, $this); |
156
|
348 |
|
} |
157
|
348 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Checks if given schema has been already visited. |
161
|
|
|
* |
162
|
|
|
* @param stdClass $schema |
163
|
|
|
* @param array $stack |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
363 |
|
private function isLooping(stdClass $schema, array &$stack) |
168
|
|
|
{ |
169
|
363 |
|
$schemaHash = spl_object_hash($schema); |
170
|
363 |
|
if (isset($stack[$schemaHash])) { |
171
|
20 |
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
363 |
|
$stack[$schemaHash] = true; |
175
|
363 |
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns constraints for current schema version. |
180
|
|
|
* |
181
|
|
|
* @param stdClass $schema |
182
|
|
|
* @param Context $context |
183
|
|
|
* |
184
|
|
|
* @return Constraint[] |
185
|
|
|
*/ |
186
|
357 |
|
private function getConstraints(stdClass $schema, Context $context) |
187
|
|
|
{ |
188
|
357 |
|
if (isset($schema->{'$schema'})) { |
189
|
4 |
|
$context->setVersion($schema->{'$schema'}); |
190
|
4 |
|
} |
191
|
|
|
|
192
|
357 |
|
return $this->registry->getConstraints($context->getVersion()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns constraints which should be applied to given instance. |
197
|
|
|
* |
198
|
|
|
* @param stdClass $schema |
199
|
|
|
* @param Context $context |
200
|
|
|
* @param mixed $instance |
201
|
|
|
* |
202
|
|
|
* @return Constraint[] |
203
|
|
|
*/ |
204
|
348 |
|
private function getConstraintsForInstance(stdClass $schema, Context $context, $instance) |
205
|
|
|
{ |
206
|
348 |
|
if (isset($schema->{'$schema'})) { |
207
|
4 |
|
$context->setVersion($schema->{'$schema'}); |
208
|
4 |
|
} |
209
|
|
|
|
210
|
348 |
|
$instanceType = gettype($instance); |
211
|
348 |
|
$schemaHash = spl_object_hash($schema); |
212
|
348 |
|
$constraintsA = & $this->constraintsCacheA[$schemaHash.$instanceType]; |
213
|
|
|
|
214
|
348 |
|
if ($constraintsA === null) { |
215
|
348 |
|
$version = $context->getVersion(); |
216
|
348 |
|
$constraintsB = & $this->constraintsCacheB[$version.$instanceType]; |
217
|
|
|
|
218
|
348 |
|
if ($constraintsB === null) { |
219
|
348 |
|
$instanceType = Types::getPrimitiveTypeOf($instance); |
220
|
348 |
|
$constraintsB = []; |
221
|
348 |
|
foreach ($this->registry->getConstraints($version) as $constraint) { |
222
|
348 |
|
if ($constraint->supports($instanceType)) { |
223
|
348 |
|
$constraintsB[] = $constraint; |
224
|
348 |
|
} |
225
|
348 |
|
} |
226
|
348 |
|
} |
227
|
|
|
|
228
|
348 |
|
$constraintsA = []; |
229
|
348 |
|
foreach ($constraintsB as $constraint) { |
230
|
348 |
|
foreach ($constraint->keywords() as $keyword) { |
231
|
348 |
|
if (property_exists($schema, $keyword)) { |
232
|
307 |
|
$constraintsA[] = $constraint; |
233
|
307 |
|
break; |
234
|
|
|
} |
235
|
348 |
|
} |
236
|
348 |
|
} |
237
|
348 |
|
} |
238
|
|
|
|
239
|
348 |
|
return $constraintsA; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|