14
14
use JsonSchema \Validator ;
15
15
use JsonSchema \Exception \InvalidSchemaMediaTypeException ;
16
16
use JsonSchema \Exception \JsonDecodingException ;
17
- use JsonSchema \Exception \UriResolverException ;
18
17
19
18
/**
20
19
* Retrieves JSON Schema URIs
23
22
*/
24
23
class UriRetriever
25
24
{
25
+ /**
26
+ * @var null|UriRetrieverInterface
27
+ */
26
28
protected $ uriRetriever = null ;
27
29
30
+ /**
31
+ * @var array|object[]
32
+ * @see loadSchema
33
+ */
34
+ private $ schemaCache = array ();
35
+
28
36
/**
29
37
* Guarantee the correct media type was encountered
30
38
*
31
- * @throws InvalidSchemaMediaTypeException
39
+ * @param UriRetrieverInterface $uriRetriever
40
+ * @param $uri
41
+ * @return bool|void
32
42
*/
33
43
public function confirmMediaType ($ uriRetriever , $ uri )
34
44
{
@@ -120,8 +130,8 @@ public function resolvePointer($jsonSchema, $uri)
120
130
* Retrieve a URI
121
131
*
122
132
* @param string $uri JSON Schema URI
133
+ * @param null $baseUri
123
134
* @return object JSON Schema contents
124
- * @throws InvalidSchemaMediaType for invalid media tyeps
125
135
*/
126
136
public function retrieve ($ uri , $ baseUri = null )
127
137
{
@@ -241,7 +251,7 @@ public function generate(array $components)
241
251
* Resolves a URI
242
252
*
243
253
* @param string $uri Absolute or relative
244
- * @param type $baseUri Optional base URI
254
+ * @param string $baseUri Optional base URI
245
255
* @return string
246
256
*/
247
257
public function resolve ($ uri , $ baseUri = null )
@@ -256,57 +266,11 @@ public function resolve($uri, $baseUri = null)
256
266
$ baseComponents = $ this ->parse ($ baseUri );
257
267
$ basePath = $ baseComponents ['path ' ];
258
268
259
- $ baseComponents ['path ' ] = self ::combineRelativePathWithBasePath ($ path , $ basePath );
269
+ $ baseComponents ['path ' ] = UriResolver ::combineRelativePathWithBasePath ($ path , $ basePath );
260
270
261
271
return $ this ->generate ($ baseComponents );
262
272
}
263
273
264
- /**
265
- * Tries to glue a relative path onto an absolute one
266
- *
267
- * @param string $relativePath
268
- * @param string $basePath
269
- * @return string Merged path
270
- * @throws UriResolverException
271
- */
272
- private static function combineRelativePathWithBasePath ($ relativePath , $ basePath )
273
- {
274
- $ relativePath = self ::normalizePath ($ relativePath );
275
- if ($ relativePath == '' ) {
276
- return $ basePath ;
277
- }
278
- if ($ relativePath [0 ] == '/ ' ) {
279
- return $ relativePath ;
280
- }
281
-
282
- $ basePathSegments = explode ('/ ' , $ basePath );
283
-
284
- preg_match ('|^/?(\.\./(?:\./)*)*| ' , $ relativePath , $ match );
285
- $ numLevelUp = strlen ($ match [0 ]) /3 + 1 ;
286
- if ($ numLevelUp >= count ($ basePathSegments )) {
287
- throw new UriResolverException (sprintf ("Unable to resolve URI '%s' from base '%s' " , $ relativePath , $ basePath ));
288
- }
289
-
290
- $ basePathSegments = array_slice ($ basePathSegments , 0 , -$ numLevelUp );
291
- $ path = preg_replace ('|^/?(\.\./(\./)*)*| ' , '' , $ relativePath );
292
-
293
- return implode ('/ ' , $ basePathSegments ) . '/ ' . $ path ;
294
- }
295
-
296
- /**
297
- * Normalizes a URI path component by removing dot-slash and double slashes
298
- *
299
- * @param string $path
300
- * @return string
301
- */
302
- private static function normalizePath ($ path )
303
- {
304
- $ path = preg_replace ('|((?<!\.)\./)*| ' , '' , $ path );
305
- $ path = preg_replace ('|//| ' , '/ ' , $ path );
306
-
307
- return $ path ;
308
- }
309
-
310
274
/**
311
275
* @param string $uri
312
276
* @return boolean
0 commit comments