]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / justinrainbow / json-schema / src / JsonSchema / SchemaStorage.php
1 <?php
2
3 namespace JsonSchema;
4
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;
11
12 class SchemaStorage implements SchemaStorageInterface
13 {
14     const INTERNAL_PROVIDED_SCHEMA_URI = 'internal://provided-schema';
15
16     protected $uriRetriever;
17     protected $uriResolver;
18     protected $schemas = array();
19
20     public function __construct(
21         UriRetrieverInterface $uriRetriever = null,
22         UriResolverInterface $uriResolver = null
23     ) {
24         $this->uriRetriever = $uriRetriever ?: new UriRetriever();
25         $this->uriResolver = $uriResolver ?: new UriResolver();
26     }
27
28     /**
29      * @return UriRetrieverInterface
30      */
31     public function getUriRetriever()
32     {
33         return $this->uriRetriever;
34     }
35
36     /**
37      * @return UriResolverInterface
38      */
39     public function getUriResolver()
40     {
41         return $this->uriResolver;
42     }
43
44     /**
45      * {@inheritdoc}
46      */
47     public function addSchema($id, $schema = null)
48     {
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);
54         }
55
56         // cast array schemas to object
57         if (is_array($schema)) {
58             $schema = BaseConstraint::arrayToObjectRecursive($schema);
59         }
60
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';
69             }
70         }
71
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;
77             }
78         }
79         $this->schemas[$id] = $schema;
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     public function getSchema($id)
86     {
87         if (!array_key_exists($id, $this->schemas)) {
88             $this->addSchema($id);
89         }
90
91         return $this->schemas[$id];
92     }
93
94     /**
95      * {@inheritdoc}
96      */
97     public function resolveRef($ref)
98     {
99         $jsonPointer = new JsonPointer($ref);
100
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()
107             ));
108         }
109
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]);
117             } else {
118                 throw new UnresolvableJsonPointerException(sprintf(
119                     'File: %s is found, but could not resolve fragment: %s',
120                     $jsonPointer->getFilename(),
121                     $jsonPointer->getPropertyPathAsString()
122                 ));
123             }
124         }
125
126         return $refSchema;
127     }
128
129     /**
130      * {@inheritdoc}
131      */
132     public function resolveRefSchema($refSchema)
133     {
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'});
138         }
139
140         return $refSchema;
141     }
142 }