1414use  JsonSchema \Validator ;
1515use  JsonSchema \Exception \InvalidSchemaMediaTypeException ;
1616use  JsonSchema \Exception \JsonDecodingException ;
17+ use  JsonSchema \Exception \ResourceNotFoundException ;
1718
1819/** 
1920 * Retrieves JSON Schema URIs 
2223 */ 
2324class  UriRetriever
2425{
26+     /** 
27+      * @var null|UriRetrieverInterface 
28+      */ 
2529    protected  $ uriRetriever  = null ;
2630
31+     /** 
32+      * @var array|object[] 
33+      * @see loadSchema 
34+      */ 
35+     private  $ schemaCache  = array ();
36+ 
2737    /** 
2838     * Guarantee the correct media type was encountered 
2939     * 
30-      * @throws InvalidSchemaMediaTypeException 
40+      * @param UriRetrieverInterface $uriRetriever 
41+      * @param string $uri 
42+      * @return bool|void 
3143     */ 
3244    public  function  confirmMediaType ($ uriRetriever , $ uri )
3345    {
@@ -78,13 +90,13 @@ public function getUriRetriever()
7890     * @param string $uri JSON Schema URI 
7991     * @return object JSON Schema after walking down the fragment pieces 
8092     * 
81-      * @throws \JsonSchema\Exception\ ResourceNotFoundException 
93+      * @throws ResourceNotFoundException 
8294     */ 
8395    public  function  resolvePointer ($ jsonSchema , $ uri )
8496    {
8597        $ resolver  = new  UriResolver ();
8698        $ parsed  = $ resolver ->parse ($ uri );
87-         if  (empty ($ parsed ['fragment ' ]))   {
99+         if  (empty ($ parsed ['fragment ' ])) {
88100            return  $ jsonSchema ;
89101        }
90102
@@ -97,14 +109,14 @@ public function resolvePointer($jsonSchema, $uri)
97109                if  (! empty ($ jsonSchema ->$ pathElement )) {
98110                    $ jsonSchema  = $ jsonSchema ->$ pathElement ;
99111                } else  {
100-                     throw  new  \ JsonSchema \ Exception \ ResourceNotFoundException (
112+                     throw  new  ResourceNotFoundException (
101113                        'Fragment " '  . $ parsed ['fragment ' ] . '" not found ' 
102114                        . ' in  '  . $ uri
103115                    );
104116                }
105117
106118                if  (! is_object ($ jsonSchema )) {
107-                     throw  new  \ JsonSchema \ Exception \ ResourceNotFoundException (
119+                     throw  new  ResourceNotFoundException (
108120                        'Fragment part " '  . $ pathElement  . '" is no object  ' 
109121                        . ' in  '  . $ uri
110122                    );
@@ -119,8 +131,8 @@ public function resolvePointer($jsonSchema, $uri)
119131     * Retrieve a URI 
120132     * 
121133     * @param string $uri JSON Schema URI 
134+      * @param string|null $baseUri 
122135     * @return object JSON Schema contents 
123-      * @throws InvalidSchemaMediaType for invalid media tyeps 
124136     */ 
125137    public  function  retrieve ($ uri , $ baseUri  = null )
126138    {
@@ -167,6 +179,7 @@ protected function loadSchema($fetchUri)
167179        }
168180
169181        $ this  ->schemaCache [$ fetchUri ] = $ jsonSchema ;
182+ 
170183        return  $ jsonSchema ;
171184    }
172185
@@ -240,7 +253,7 @@ public function generate(array $components)
240253     * Resolves a URI 
241254     * 
242255     * @param string $uri Absolute or relative 
243-      * @param type  $baseUri Optional base URI 
256+      * @param string  $baseUri Optional base URI 
244257     * @return string 
245258     */ 
246259    public  function  resolve ($ uri , $ baseUri  = null )
@@ -255,58 +268,11 @@ public function resolve($uri, $baseUri = null)
255268        $ baseComponents  = $ this  ->parse ($ baseUri );
256269        $ basePath  = $ baseComponents ['path ' ];
257270
258-         $ baseComponents ['path ' ] = self ::combineRelativePathWithBasePath ($ path , $ basePath );
271+         $ baseComponents ['path ' ] = UriResolver ::combineRelativePathWithBasePath ($ path , $ basePath );
259272
260273        return  $ this  ->generate ($ baseComponents );
261274    }
262275
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- 
310276    /** 
311277     * @param string $uri 
312278     * @return boolean 
0 commit comments