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 | public function __construct(LoaderManager $loaderManager = null) |
||
25 | 168 | ||
26 | 168 | /** |
|
27 | 168 | * 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 | public function dereference($schema) |
||
46 | |||
47 | 136 | /** |
|
48 | * @return LoaderManager |
||
49 | */ |
||
50 | public function getLoaderManager() |
||
54 | |||
55 | /** |
||
56 | 122 | * Crawl the schema and resolve any references. |
|
57 | * |
||
58 | 122 | * @param object $schema |
|
59 | 122 | * @param string|null $currentUri |
|
60 | * |
||
61 | * @return object |
||
62 | */ |
||
63 | private function crawl($schema, $currentUri = null) |
||
75 | |||
76 | /** |
||
77 | * @param object $schema |
||
78 | * @param string $path |
||
79 | 46 | * @param string $ref |
|
80 | * @param string $currentUri |
||
81 | 46 | */ |
|
82 | 2 | private function resolveReference($schema, $path, $ref, $currentUri) |
|
99 | |||
100 | /** |
||
101 | * Resolve the external reference at the given path. |
||
102 | 168 | * |
|
103 | * @param object $schema The JSON Schema |
||
104 | 168 | * @param string $path A JSON pointer to the $ref's location in the schema. |
|
105 | 168 | * @param string $ref The JSON reference |
|
106 | 168 | * @param string|null $currentUri The URI of the schema, or null if the schema was loaded from an object. |
|
107 | 168 | * |
|
108 | * @return object The schema with the reference resolved. |
||
109 | */ |
||
110 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
||
117 | |||
118 | /** |
||
119 | * Merge the resolved reference with the schema, at the given path. |
||
120 | * |
||
121 | 164 | * @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 | 160 | * |
|
125 | 164 | * @return void |
|
126 | */ |
||
127 | 164 | private function mergeResolvedReference($schema, $resolved, $path) |
|
142 | |||
143 | 54 | /** |
|
144 | 26 | * Check if the reference contains a fragment and resolve |
|
145 | 26 | * the pointer. Otherwise returns the original schema. |
|
146 | 26 | * |
|
147 | 26 | * @param string $ref |
|
148 | 44 | * @param object $schema |
|
149 | * |
||
150 | * @return object |
||
151 | */ |
||
152 | 54 | private function resolveFragment($ref, $schema) |
|
165 | |||
166 | /** |
||
167 | * @param string $attribute |
||
168 | 26 | * @param mixed $attributeValue |
|
169 | * |
||
170 | 26 | * @return bool |
|
171 | 26 | */ |
|
172 | 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 | private function loadExternalRef($reference) |
||
192 | 16 | ||
193 | 16 | /** |
|
194 | 50 | * Merge a resolved reference into the root of the given schema. |
|
195 | 50 | * |
|
196 | 50 | * @param object $rootSchema |
|
197 | 50 | * @param object $resolvedRef |
|
198 | */ |
||
199 | 52 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
207 | |||
208 | /** |
||
209 | * Take a relative reference, and prepend the id of the schema and any |
||
210 | 56 | * sub schemas to get the absolute url. |
|
211 | * |
||
212 | 56 | * @param object $schema |
|
213 | 56 | * @param string $path |
|
214 | 10 | * @param string $ref |
|
215 | 6 | * @param string|null $currentUri |
|
216 | 6 | * |
|
217 | 10 | * @return string |
|
218 | 10 | */ |
|
219 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
||
231 | |||
232 | 160 | /** |
|
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 | 48 | * @return string |
|
243 | */ |
||
244 | 48 | private function getResolvedResolutionScope($schema, $path, $scope) |
|
263 | } |
||
264 |