1 | <?php |
||
9 | class Dereferencer |
||
10 | { |
||
11 | /** |
||
12 | * @var LoaderManager |
||
13 | */ |
||
14 | private $loaderManager; |
||
15 | |||
16 | /** |
||
17 | * Create a new Dereferencer. |
||
18 | * |
||
19 | * @param LoaderManager $loaderManager |
||
20 | */ |
||
21 | 164 | public function __construct(LoaderManager $loaderManager = null) |
|
25 | |||
26 | /** |
||
27 | * Return the schema with all references resolved. |
||
28 | * |
||
29 | * @param string|object $schema Either a valid path like "http://json-schema.org/draft-03/schema#" |
||
30 | * or the object resulting from a json_decode call. |
||
31 | * |
||
32 | * @return object |
||
33 | */ |
||
34 | 164 | public function dereference($schema) |
|
46 | |||
47 | /** |
||
48 | * @return LoaderManager |
||
49 | */ |
||
50 | 122 | public function getLoaderManager() |
|
54 | |||
55 | /** |
||
56 | * Crawl the schema and resolve any references. |
||
57 | * |
||
58 | * @param object $schema |
||
59 | * @param string|null $currentUri |
||
60 | * |
||
61 | * @return object |
||
62 | */ |
||
63 | 164 | private function crawl($schema, $currentUri = null) |
|
75 | |||
76 | /** |
||
77 | * @param object $schema |
||
78 | * @param string $path |
||
79 | * @param string $ref |
||
80 | * @param string $currentUri |
||
81 | */ |
||
82 | 54 | private function resolveReference($schema, $path, $ref, $currentUri) |
|
99 | |||
100 | /** |
||
101 | * Resolve the external reference at the given path. |
||
102 | * |
||
103 | * @param object $schema The JSON Schema |
||
104 | * @param string $path A JSON pointer to the $ref's location in the schema. |
||
105 | * @param string $ref The JSON reference |
||
106 | * @param string|null $currentUri The URI of the schema, or null if the schema was loaded from an object. |
||
107 | * |
||
108 | * @return object The schema with the reference resolved. |
||
109 | */ |
||
110 | 26 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
|
117 | |||
118 | /** |
||
119 | * Merge the resolved reference with the schema, at the given path. |
||
120 | * |
||
121 | * @param object $schema The schema to merge the resolved reference with |
||
122 | * @param object $resolved The resolved schema |
||
123 | * @param string $path A JSON pointer to the path where the reference should be merged. |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | 54 | private function mergeResolvedReference($schema, $resolved, $path) |
|
142 | |||
143 | /** |
||
144 | * Check if the reference contains a fragment and resolve |
||
145 | * the pointer. Otherwise returns the original schema. |
||
146 | * |
||
147 | * @param string $ref |
||
148 | * @param object $schema |
||
149 | * |
||
150 | * @return object |
||
151 | */ |
||
152 | 56 | private function resolveFragment($ref, $schema) |
|
165 | |||
166 | /** |
||
167 | * @param string $attribute |
||
168 | * @param mixed $attributeValue |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | 160 | private function isRef($attribute, $attributeValue) |
|
176 | |||
177 | /** |
||
178 | * Load an external ref and return the JSON object. |
||
179 | * |
||
180 | * @param string $reference |
||
181 | * |
||
182 | * @return object |
||
183 | */ |
||
184 | 46 | private function loadExternalRef($reference) |
|
192 | |||
193 | /** |
||
194 | * Merge a resolved reference into the root of the given schema. |
||
195 | * |
||
196 | * @param object $rootSchema |
||
197 | * @param object $resolvedRef |
||
198 | */ |
||
199 | 16 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
207 | |||
208 | /** |
||
209 | * Take a relative reference, and prepend the id of the schema and any |
||
210 | * sub schemas to get the absolute url. |
||
211 | * |
||
212 | * @param object $schema |
||
213 | * @param string $path |
||
214 | * @param string $ref |
||
215 | * @param string|null $currentUri |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 26 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
|
231 | |||
232 | /** |
||
233 | * Get the resolved resolution scope by walking the schema and resolving |
||
234 | * every `id` against the most immediate parent scope. |
||
235 | * |
||
236 | * @see http://json-schema.org/latest/json-schema-core.html#anchor27 |
||
237 | * |
||
238 | * @param object $schema |
||
239 | * @param string $path |
||
240 | * @param string $scope |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | 18 | private function getResolvedResolutionScope($schema, $path, $scope) |
|
263 | } |
||
264 |