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
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param Registry $registry |
48
|
|
|
* @param Resolver $resolver |
49
|
|
|
*/ |
50
|
485 |
|
public function __construct(Registry $registry, Resolver $resolver) |
51
|
|
|
{ |
52
|
485 |
|
$this->registry = $registry; |
53
|
485 |
|
$this->resolver = $resolver; |
54
|
485 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Recursively resolve JSON pointer references within a given schema. |
58
|
|
|
* |
59
|
|
|
* @param stdClass $schema The schema to resolve |
60
|
|
|
* @param Uri $uri The URI of the schema |
61
|
|
|
* |
62
|
|
|
* @return stdClass |
63
|
|
|
*/ |
64
|
299 |
|
public function resolveReferences(stdClass $schema, Uri $uri) |
65
|
|
|
{ |
66
|
299 |
|
$this->resolver->setBaseSchema($schema, $uri); |
67
|
299 |
|
$schema = $this->doResolveReferences($schema, $uri); |
68
|
299 |
|
$this->resolver->clearStack(); |
69
|
|
|
|
70
|
299 |
|
return $schema; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
299 |
|
private function doResolveReferences(stdClass $schema, Uri $uri) |
75
|
|
|
{ |
76
|
299 |
|
if ($this->isLooping($schema, $this->resolvedSchemas)) { |
77
|
20 |
|
return $schema; |
78
|
|
|
} |
79
|
|
|
|
80
|
299 |
|
$inScope = false; |
81
|
299 |
|
if (property_exists($schema, 'id') && is_string($schema->id)) { |
82
|
7 |
|
$this->resolver->enter(new Uri($schema->id)); |
83
|
7 |
|
$inScope = true; |
84
|
7 |
|
} |
85
|
|
|
|
86
|
299 |
|
if (property_exists($schema, '$ref')) { |
87
|
22 |
|
$resolved = $this->resolver->resolve($schema); |
88
|
22 |
|
$this->resolver->enter($resolved[0], $resolved[1]); |
89
|
22 |
|
$schema = $this->doResolveReferences($resolved[1], $resolved[0]); |
90
|
22 |
|
$this->resolver->leave(); |
91
|
22 |
|
} else { |
92
|
299 |
|
foreach ($schema as $property => $value) { |
93
|
299 |
|
if (is_object($value)) { |
94
|
114 |
|
$schema->{$property} = $this->doResolveReferences($value, $uri); |
95
|
299 |
|
} elseif (is_array($value)) { |
96
|
75 |
|
foreach ($value as $index => $element) { |
97
|
73 |
|
if (is_object($element)) { |
98
|
44 |
|
$schema->{$property}[$index] = $this->doResolveReferences($element, $uri); |
99
|
44 |
|
} |
100
|
75 |
|
} |
101
|
75 |
|
} |
102
|
299 |
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
299 |
|
if ($inScope) { |
106
|
7 |
|
$this->resolver->leave(); |
107
|
7 |
|
} |
108
|
|
|
|
109
|
299 |
|
return $schema; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Recursively normalizes a given schema and validates its syntax. |
114
|
|
|
* |
115
|
|
|
* @param stdClass $schema |
116
|
|
|
* @param Context $context |
117
|
|
|
* |
118
|
|
|
* @return stdClass |
119
|
|
|
*/ |
120
|
357 |
|
public function parseSchema(stdClass $schema, Context $context) |
121
|
|
|
{ |
122
|
357 |
|
if ($this->isLooping($schema, $this->parsedSchemas)) { |
123
|
12 |
|
return $schema; |
124
|
|
|
} |
125
|
|
|
|
126
|
357 |
|
foreach ($this->getConstraints($schema, $context) as $constraint) { |
127
|
357 |
|
foreach ($constraint->keywords() as $keyword) { |
128
|
357 |
|
if (property_exists($schema, $keyword)) { |
129
|
333 |
|
$constraint->normalize($schema, $context, $this); |
130
|
333 |
|
break; |
131
|
|
|
} |
132
|
357 |
|
} |
133
|
357 |
|
} |
134
|
|
|
|
135
|
357 |
|
return $schema; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Validates an instance against a given schema, populating a context |
140
|
|
|
* with encountered violations. |
141
|
|
|
* |
142
|
|
|
* @param $instance |
143
|
|
|
* @param stdClass $schema |
144
|
|
|
* @param Context $context |
145
|
|
|
*/ |
146
|
348 |
|
public function applyConstraints($instance, stdClass $schema, Context $context) |
147
|
|
|
{ |
148
|
348 |
|
$instanceType = Types::getPrimitiveTypeOf($instance); |
149
|
|
|
|
150
|
348 |
|
foreach ($this->getConstraints($schema, $context) as $constraint) { |
151
|
348 |
|
foreach ($constraint->keywords() as $keyword) { |
152
|
348 |
|
if ($constraint->supports($instanceType)) { |
153
|
348 |
|
if (property_exists($schema, $keyword)) { |
154
|
307 |
|
$constraint->apply($instance, $schema, $context, $this); |
155
|
307 |
|
break; |
156
|
|
|
} |
157
|
348 |
|
} |
158
|
348 |
|
} |
159
|
348 |
|
} |
160
|
348 |
|
} |
161
|
|
|
|
162
|
363 |
|
private function isLooping($item, array &$stack) |
163
|
|
|
{ |
164
|
363 |
|
$isKnown = false; |
165
|
|
|
|
166
|
363 |
|
foreach ($stack as $knownItem) { |
167
|
202 |
|
if ($item === $knownItem) { |
168
|
20 |
|
$isKnown = true; |
169
|
20 |
|
break; |
170
|
|
|
} |
171
|
363 |
|
} |
172
|
|
|
|
173
|
363 |
|
if (!$isKnown) { |
174
|
363 |
|
$stack[] = $item; |
175
|
363 |
|
} |
176
|
|
|
|
177
|
363 |
|
return $isKnown; |
178
|
|
|
} |
179
|
|
|
|
180
|
357 |
|
private function getConstraints(stdClass $schema, Context $context) |
181
|
|
|
{ |
182
|
357 |
|
if (property_exists($schema, '$schema')) { |
183
|
4 |
|
$context->setVersion($schema->{'$schema'}); |
184
|
4 |
|
} |
185
|
|
|
|
186
|
357 |
|
return $this->registry->getConstraints($context->getVersion()); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|