5 use JsonSchema\Constraints\BaseConstraint;
6 use JsonSchema\Entity\JsonPointer;
7 use JsonSchema\Exception\UnresolvableJsonPointerException;
8 use JsonSchema\Iterator\ObjectIterator;
9 use JsonSchema\Uri\UriResolver;
10 use JsonSchema\Uri\UriRetriever;
12 class SchemaStorage implements SchemaStorageInterface
14 const INTERNAL_PROVIDED_SCHEMA_URI = 'internal://provided-schema';
16 protected $uriRetriever;
17 protected $uriResolver;
18 protected $schemas = array();
20 public function __construct(
21 UriRetrieverInterface $uriRetriever = null,
22 UriResolverInterface $uriResolver = null
24 $this->uriRetriever = $uriRetriever ?: new UriRetriever();
25 $this->uriResolver = $uriResolver ?: new UriResolver();
29 * @return UriRetrieverInterface
31 public function getUriRetriever()
33 return $this->uriRetriever;
37 * @return UriResolverInterface
39 public function getUriResolver()
41 return $this->uriResolver;
47 public function addSchema($id, $schema = null)
49 if (is_null($schema) && $id !== self::INTERNAL_PROVIDED_SCHEMA_URI) {
50 // if the schema was user-provided to Validator and is still null, then assume this is
51 // what the user intended, as there's no way for us to retrieve anything else. User-supplied
52 // schemas do not have an associated URI when passed via Validator::validate().
53 $schema = $this->uriRetriever->retrieve($id);
56 // cast array schemas to object
57 if (is_array($schema)) {
58 $schema = BaseConstraint::arrayToObjectRecursive($schema);
61 // workaround for bug in draft-03 & draft-04 meta-schemas (id & $ref defined with incorrect format)
62 // see https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367
63 if (is_object($schema) && property_exists($schema, 'id')) {
64 if ($schema->id == 'http://json-schema.org/draft-04/schema#') {
65 $schema->properties->id->format = 'uri-reference';
66 } elseif ($schema->id == 'http://json-schema.org/draft-03/schema#') {
67 $schema->properties->id->format = 'uri-reference';
68 $schema->properties->{'$ref'}->format = 'uri-reference';
72 $objectIterator = new ObjectIterator($schema);
73 foreach ($objectIterator as $toResolveSchema) {
74 if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) {
75 $jsonPointer = new JsonPointer($this->uriResolver->resolve($toResolveSchema->{'$ref'}, $id));
76 $toResolveSchema->{'$ref'} = (string) $jsonPointer;
79 $this->schemas[$id] = $schema;
85 public function getSchema($id)
87 if (!array_key_exists($id, $this->schemas)) {
88 $this->addSchema($id);
91 return $this->schemas[$id];
97 public function resolveRef($ref)
99 $jsonPointer = new JsonPointer($ref);
101 // resolve filename for pointer
102 $fileName = $jsonPointer->getFilename();
103 if (!strlen($fileName)) {
104 throw new UnresolvableJsonPointerException(sprintf(
105 "Could not resolve fragment '%s': no file is defined",
106 $jsonPointer->getPropertyPathAsString()
110 // get & process the schema
111 $refSchema = $this->getSchema($fileName);
112 foreach ($jsonPointer->getPropertyPaths() as $path) {
113 if (is_object($refSchema) && property_exists($refSchema, $path)) {
114 $refSchema = $this->resolveRefSchema($refSchema->{$path});
115 } elseif (is_array($refSchema) && array_key_exists($path, $refSchema)) {
116 $refSchema = $this->resolveRefSchema($refSchema[$path]);
118 throw new UnresolvableJsonPointerException(sprintf(
119 'File: %s is found, but could not resolve fragment: %s',
120 $jsonPointer->getFilename(),
121 $jsonPointer->getPropertyPathAsString()
132 public function resolveRefSchema($refSchema)
134 if (is_object($refSchema) && property_exists($refSchema, '$ref') && is_string($refSchema->{'$ref'})) {
135 $newSchema = $this->resolveRef($refSchema->{'$ref'});
136 $refSchema = (object) (get_object_vars($refSchema) + get_object_vars($newSchema));
137 unset($refSchema->{'$ref'});