14
14
use JsonSchema \Validator ;
15
15
use JsonSchema \Exception \InvalidSchemaMediaTypeException ;
16
16
use JsonSchema \Exception \JsonDecodingException ;
17
+ use JsonSchema \Exception \ResourceNotFoundException ;
17
18
18
19
/**
19
20
* Retrieves JSON Schema URIs
22
23
*/
23
24
class UriRetriever
24
25
{
26
+ /**
27
+ * @var null|UriRetrieverInterface
28
+ */
25
29
protected $ uriRetriever = null ;
26
30
31
+ /**
32
+ * @var array|object[]
33
+ * @see loadSchema
34
+ */
35
+ private $ schemaCache = array ();
36
+
27
37
/**
28
38
* Guarantee the correct media type was encountered
29
39
*
30
- * @throws InvalidSchemaMediaTypeException
40
+ * @param UriRetrieverInterface $uriRetriever
41
+ * @param string $uri
42
+ * @return bool|void
31
43
*/
32
44
public function confirmMediaType ($ uriRetriever , $ uri )
33
45
{
@@ -78,13 +90,13 @@ public function getUriRetriever()
78
90
* @param string $uri JSON Schema URI
79
91
* @return object JSON Schema after walking down the fragment pieces
80
92
*
81
- * @throws \JsonSchema\Exception\ ResourceNotFoundException
93
+ * @throws ResourceNotFoundException
82
94
*/
83
95
public function resolvePointer ($ jsonSchema , $ uri )
84
96
{
85
97
$ resolver = new UriResolver ();
86
98
$ parsed = $ resolver ->parse ($ uri );
87
- if (empty ($ parsed ['fragment ' ])) {
99
+ if (empty ($ parsed ['fragment ' ])) {
88
100
return $ jsonSchema ;
89
101
}
90
102
@@ -97,14 +109,14 @@ public function resolvePointer($jsonSchema, $uri)
97
109
if (! empty ($ jsonSchema ->$ pathElement )) {
98
110
$ jsonSchema = $ jsonSchema ->$ pathElement ;
99
111
} else {
100
- throw new \ JsonSchema \ Exception \ ResourceNotFoundException (
112
+ throw new ResourceNotFoundException (
101
113
'Fragment " ' . $ parsed ['fragment ' ] . '" not found '
102
114
. ' in ' . $ uri
103
115
);
104
116
}
105
117
106
118
if (! is_object ($ jsonSchema )) {
107
- throw new \ JsonSchema \ Exception \ ResourceNotFoundException (
119
+ throw new ResourceNotFoundException (
108
120
'Fragment part " ' . $ pathElement . '" is no object '
109
121
. ' in ' . $ uri
110
122
);
@@ -119,8 +131,8 @@ public function resolvePointer($jsonSchema, $uri)
119
131
* Retrieve a URI
120
132
*
121
133
* @param string $uri JSON Schema URI
134
+ * @param string|null $baseUri
122
135
* @return object JSON Schema contents
123
- * @throws InvalidSchemaMediaType for invalid media tyeps
124
136
*/
125
137
public function retrieve ($ uri , $ baseUri = null )
126
138
{
@@ -167,6 +179,7 @@ protected function loadSchema($fetchUri)
167
179
}
168
180
169
181
$ this ->schemaCache [$ fetchUri ] = $ jsonSchema ;
182
+
170
183
return $ jsonSchema ;
171
184
}
172
185
@@ -240,7 +253,7 @@ public function generate(array $components)
240
253
* Resolves a URI
241
254
*
242
255
* @param string $uri Absolute or relative
243
- * @param type $baseUri Optional base URI
256
+ * @param string $baseUri Optional base URI
244
257
* @return string
245
258
*/
246
259
public function resolve ($ uri , $ baseUri = null )
@@ -255,58 +268,11 @@ public function resolve($uri, $baseUri = null)
255
268
$ baseComponents = $ this ->parse ($ baseUri );
256
269
$ basePath = $ baseComponents ['path ' ];
257
270
258
- $ baseComponents ['path ' ] = self ::combineRelativePathWithBasePath ($ path , $ basePath );
271
+ $ baseComponents ['path ' ] = UriResolver ::combineRelativePathWithBasePath ($ path , $ basePath );
259
272
260
273
return $ this ->generate ($ baseComponents );
261
274
}
262
275
263
- /**
264
- * Tries to glue a relative path onto an absolute one
265
- *
266
- * @param string $relativePath
267
- * @param string $basePath
268
- * @return string Merged path
269
- * @throws UriResolverException
270
- */
271
- private static function combineRelativePathWithBasePath ($ relativePath , $ basePath )
272
- {
273
- $ relativePath = self ::normalizePath ($ relativePath );
274
- $ basePathSegments = self ::getPathSegments ($ basePath );
275
-
276
- preg_match ('|^/?(\.\./(?:\./)*)*| ' , $ relativePath , $ match );
277
- $ numLevelUp = strlen ($ match [0 ]) /3 + 1 ;
278
- if ($ numLevelUp >= count ($ basePathSegments )) {
279
- throw new \JsonSchema \Exception \UriResolverException (sprintf ("Unable to resolve URI '%s' from base '%s' " , $ relativePath , $ basePath ));
280
- }
281
-
282
- $ basePathSegments = array_slice ($ basePathSegments , 0 , -$ numLevelUp );
283
- $ path = preg_replace ('|^/?(\.\./(\./)*)*| ' , '' , $ relativePath );
284
-
285
- return implode ('/ ' , $ basePathSegments ) . '/ ' . $ path ;
286
- }
287
-
288
- /**
289
- * Normalizes a URI path component by removing dot-slash and double slashes
290
- *
291
- * @param string $path
292
- * @return string
293
- */
294
- private static function normalizePath ($ path )
295
- {
296
- $ path = preg_replace ('|((?<!\.)\./)*| ' , '' , $ path );
297
- $ path = preg_replace ('|//| ' , '/ ' , $ path );
298
-
299
- return $ path ;
300
- }
301
-
302
- /**
303
- * @return array
304
- */
305
- private static function getPathSegments ($ path )
306
- {
307
- return explode ('/ ' , $ path );
308
- }
309
-
310
276
/**
311
277
* @param string $uri
312
278
* @return boolean
0 commit comments