public function resolveRef($ref) { $jsonPointer = new JsonPointer($ref); $refSchema = $this->getSchema($jsonPointer->getFilename()); foreach ($jsonPointer->getPropertyPaths() as $path) { if (is_object($refSchema) && property_exists($refSchema, $path)) { $refSchema = $this->resolveRefSchema($refSchema->{$path}); } elseif (is_array($refSchema) && array_key_exists($path, $refSchema)) { $refSchema = $this->resolveRefSchema($refSchema[$path]); } else { throw new UnresolvableJsonPointerException(sprintf('File: %s is found, but could not resolve fragment: %s', $jsonPointer->getFilename(), $jsonPointer->getPropertyPathAsString())); } } return $refSchema; }
/** * @param JsonPointer $pointer * @return string property path */ protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer) { $result = array_map(function ($path) { return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path); }, $pointer->getPropertyPaths()); return trim(implode('', $result), '.'); }
/** * @param JsonPointer $jsonPointer * @param object $refSchema * @throws UnresolvableJsonPointerException when json schema file is found but reference can not be resolved * @return object */ private function getRefSchema(JsonPointer $jsonPointer, $refSchema) { foreach ($jsonPointer->getPropertyPaths() as $path) { if (is_object($refSchema) && property_exists($refSchema, $path)) { $refSchema = $refSchema->{$path}; } elseif (is_array($refSchema) && array_key_exists($path, $refSchema)) { $refSchema = $refSchema[$path]; } else { throw new UnresolvableJsonPointerException(sprintf('File: %s is found, but could not resolve fragment: %s', $jsonPointer->getFilename(), $jsonPointer->getPropertyPathAsString())); } } return $refSchema; }