1 | <?php |
||
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) |
|
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) |
|
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) |
|
161 | |||
162 | 363 | private function isLooping($item, array &$stack) |
|
179 | |||
180 | 357 | private function getConstraints(stdClass $schema, Context $context) |
|
188 | } |
||
189 |