/**
  * Resolve a schema based on pointer
  *
  * URIs can have a fragment at the end in the format of
  * #/path/to/object and we are to look up the 'path' property of
  * the first object then the 'to' and 'object' properties.
  *
  * @param object $jsonSchema JSON Schema contents
  * @param string $uri JSON Schema URI
  * @return object JSON Schema after walking down the fragment pieces
  */
 public function resolvePointer($jsonSchema, $uri)
 {
     $resolver = new UriResolver();
     $parsed = $resolver->parse($uri);
     if (empty($parsed['fragment'])) {
         return $jsonSchema;
     }
     $path = explode('/', $parsed['fragment']);
     while ($path) {
         $pathElement = array_shift($path);
         if (!empty($pathElement)) {
             $pathElement = str_replace('~1', '/', $pathElement);
             $pathElement = str_replace('~0', '~', $pathElement);
             if (!empty($jsonSchema->{$pathElement})) {
                 $jsonSchema = $jsonSchema->{$pathElement};
             } else {
                 $jsonSchema = new \stdClass();
             }
             if (!is_object($jsonSchema)) {
                 $jsonSchema = new \stdClass();
             }
         }
     }
     return $jsonSchema;
 }
 public function retrieve($uri)
 {
     $resolver = new UriResolver();
     $parts = $resolver->parse($uri);
     $parts['fragment'] = '#';
     $uri = $resolver->generate($parts);
     return parent::retrieve($uri);
 }
Exemple #3
0
 public function retrieve($uri, $baseUri = null)
 {
     $resolver = new UriResolver();
     $resolvedUri = $fetchUri = $resolver->resolve($uri, $baseUri);
     $arParts = $resolver->parse($resolvedUri);
     if (isset($arParts['fragment'])) {
         unset($arParts['fragment']);
         $fetchUri = $resolver->generate($arParts);
     }
     $jsonSchema = $this->loadSchema($fetchUri);
     $jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
     $jsonSchema->id = $resolvedUri;
     return $jsonSchema;
 }
 function testParseFragmentOnly()
 {
     $resolver = new UriResolver();
     $this->assertEquals(array('scheme' => '', 'authority' => '', 'path' => '', 'query' => '', 'fragment' => '/theFragment'), $resolver->parse('#/theFragment'));
 }
Exemple #5
0
 /**
  * Retrieve a URI
  *
  * @param string $uri JSON Schema URI
  * @param string|null $baseUri
  * @return object JSON Schema contents
  */
 public function retrieve($uri, $baseUri = null)
 {
     $resolver = new UriResolver();
     $resolvedUri = $fetchUri = $resolver->resolve($uri, $baseUri);
     //fetch URL without #fragment
     $arParts = $resolver->parse($resolvedUri);
     if (isset($arParts['fragment'])) {
         unset($arParts['fragment']);
         $fetchUri = $resolver->generate($arParts);
     }
     $jsonSchema = $this->loadSchema($fetchUri, $resolvedUri);
     // Use the JSON pointer if specified
     $jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
     if ($jsonSchema instanceof \stdClass) {
         $jsonSchema->id = $resolvedUri;
     }
     return $jsonSchema;
 }