1 | <?php |
||
13 | class Dereferencer |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $loaders; |
||
19 | |||
20 | /** |
||
21 | * Create a new Dereferencer. |
||
22 | */ |
||
23 | 168 | public function __construct() |
|
28 | |||
29 | /** |
||
30 | * Return the schema with all references resolved. |
||
31 | * |
||
32 | * @param string|object $schema Either a valid path like "http://json-schema.org/draft-03/schema#" |
||
33 | * or the object resulting from a json_decode call. |
||
34 | * |
||
35 | * @return object |
||
36 | */ |
||
37 | 166 | public function dereference($schema) |
|
38 | { |
||
39 | 166 | if (is_string($schema)) { |
|
40 | 30 | $uri = $schema; |
|
41 | 30 | $schema = $this->loadExternalRef($uri); |
|
42 | 28 | $schema = $this->resolveFragment($uri, $schema); |
|
43 | |||
44 | 28 | return $this->crawl($schema, strip_fragment($uri)); |
|
45 | } |
||
46 | |||
47 | 136 | return $this->crawl($schema); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Register a Loader for the given prefix. |
||
52 | * |
||
53 | * @param Loader $loader |
||
54 | * @param string $prefix |
||
55 | */ |
||
56 | 122 | public function registerLoader(Loader $loader, $prefix) |
|
60 | |||
61 | /** |
||
62 | * Get all registered loaders, keyed by the prefix they are registered to load schemas for. |
||
63 | * |
||
64 | * @return Loader[] |
||
65 | */ |
||
66 | 2 | public function getLoaders() |
|
70 | |||
71 | /** |
||
72 | * Get the loader for the given prefix. |
||
73 | * |
||
74 | * @param string $prefix |
||
75 | * |
||
76 | * @return Loader |
||
77 | * @throws \InvalidArgumentException |
||
78 | */ |
||
79 | 46 | public function getLoader($prefix) |
|
80 | { |
||
81 | 46 | if (!array_key_exists($prefix, $this->loaders)) { |
|
82 | 2 | throw new \InvalidArgumentException(sprintf('A loader is not registered for the prefix "%s"', $prefix)); |
|
83 | } |
||
84 | |||
85 | 44 | return $this->loaders[$prefix]; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Register the default file loader. |
||
90 | */ |
||
91 | 168 | private function registerFileLoader() |
|
95 | |||
96 | /** |
||
97 | * Register the default web loaders. If the curl extension is loaded, |
||
98 | * the CurlWebLoader will be used. Otherwise the FileGetContentsWebLoader |
||
99 | * will be used. You can override this by registering your own loader |
||
100 | * for the 'http' and 'https' protocols. |
||
101 | */ |
||
102 | 168 | private function registerDefaultWebLoaders() |
|
112 | |||
113 | /** |
||
114 | * Crawl the schema and resolve any references. |
||
115 | * |
||
116 | * @param object $schema |
||
117 | * @param string|null $currentUri |
||
118 | * |
||
119 | * @return object |
||
120 | */ |
||
121 | 164 | private function crawl($schema, $currentUri = null) |
|
133 | |||
134 | /** |
||
135 | * @param object $schema |
||
136 | 54 | * @param string $path |
|
137 | * @param string $ref |
||
138 | * @param string $currentUri |
||
139 | 54 | */ |
|
140 | 162 | private function resolveReference($schema, $path, $ref, $currentUri) |
|
157 | 26 | ||
158 | 26 | /** |
|
159 | * Resolve the external reference at the given path. |
||
160 | 24 | * |
|
161 | * @param object $schema The JSON Schema |
||
162 | * @param string $path A JSON pointer to the $ref's location in the schema. |
||
163 | * @param string $ref The JSON reference |
||
164 | * @param string|null $currentUri The URI of the schema, or null if the schema was loaded from an object. |
||
165 | * |
||
166 | * @return object The schema with the reference resolved. |
||
167 | */ |
||
168 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
||
175 | |||
176 | 18 | /** |
|
177 | 18 | * Merge the resolved reference with the schema, at the given path. |
|
178 | 16 | * |
|
179 | 16 | * @param object $schema The schema to merge the resolved reference with |
|
180 | 16 | * @param object $resolved The resolved schema |
|
181 | 50 | * @param string $path A JSON pointer to the path where the reference should be merged. |
|
182 | 50 | * |
|
183 | 50 | * @return void |
|
184 | 50 | */ |
|
185 | private function mergeResolvedReference($schema, $resolved, $path) |
||
200 | 56 | ||
201 | 10 | /** |
|
202 | 6 | * Check if the reference contains a fragment and resolve |
|
203 | 6 | * the pointer. Otherwise returns the original schema. |
|
204 | 10 | * |
|
205 | 10 | * @param string $ref |
|
206 | * @param object $schema |
||
207 | * |
||
208 | 56 | * @return object |
|
209 | */ |
||
210 | private function resolveFragment($ref, $schema) |
||
223 | |||
224 | /** |
||
225 | 164 | * @param string $attribute |
|
226 | * @param mixed $attributeValue |
||
227 | 164 | * |
|
228 | * @return bool |
||
229 | 164 | */ |
|
230 | 42 | private function isRef($attribute, $attributeValue) |
|
234 | 164 | ||
235 | 164 | /** |
|
236 | 54 | * Load an external ref and return the JSON object. |
|
237 | 54 | * |
|
238 | 162 | * @param string $reference |
|
239 | 96 | * |
|
240 | 96 | * @return object |
|
241 | 160 | */ |
|
242 | 64 | private function loadExternalRef($reference) |
|
254 | |||
255 | /** |
||
256 | * Merge a resolved reference into the root of the given schema. |
||
257 | * |
||
258 | * @param object $rootSchema |
||
259 | * @param object $resolvedRef |
||
260 | */ |
||
261 | private function mergeRootRef($rootSchema, $resolvedRef) |
||
269 | |||
270 | /** |
||
271 | * Validate an absolute path is valid. |
||
272 | * |
||
273 | * @param string $path |
||
274 | 164 | */ |
|
275 | private function validateAbsolutePath($path) |
||
287 | |||
288 | /** |
||
289 | * Take a relative reference, and prepend the id of the schema and any |
||
290 | * sub schemas to get the absolute url. |
||
291 | * |
||
292 | * @param object $schema |
||
293 | * @param string $path |
||
294 | 56 | * @param string $ref |
|
295 | * @param string|null $currentUri |
||
296 | 56 | * |
|
297 | * @return string |
||
298 | */ |
||
299 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
||
311 | |||
312 | 46 | /** |
|
313 | * Get the resolved resolution scope by walking the schema and resolving |
||
314 | 44 | * every `id` against the most immediate parent scope. |
|
315 | * |
||
316 | 44 | * @see http://json-schema.org/latest/json-schema-core.html#anchor27 |
|
317 | * |
||
318 | * @param object $schema |
||
319 | * @param string $path |
||
320 | * @param string $scope |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | private function getResolvedResolutionScope($schema, $path, $scope) |
||
343 | } |
||
344 |