]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / justinrainbow / json-schema / src / JsonSchema / Constraints / Constraint.php
1 <?php
2
3 /*
4  * This file is part of the JsonSchema package.
5  *
6  * For the full copyright and license information, please view the LICENSE
7  * file that was distributed with this source code.
8  */
9
10 namespace JsonSchema\Constraints;
11
12 use JsonSchema\Entity\JsonPointer;
13 use JsonSchema\SchemaStorage;
14 use JsonSchema\Uri\UriRetriever;
15 use JsonSchema\UriRetrieverInterface;
16
17 /**
18  * The Base Constraints, all Validators should extend this class
19  *
20  * @author Robert Schönthal <seroscho@googlemail.com>
21  * @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
22  */
23 abstract class Constraint extends BaseConstraint implements ConstraintInterface
24 {
25     protected $inlineSchemaProperty = '$schema';
26
27     const CHECK_MODE_NONE =             0x00000000;
28     const CHECK_MODE_NORMAL =           0x00000001;
29     const CHECK_MODE_TYPE_CAST =        0x00000002;
30     const CHECK_MODE_COERCE_TYPES =     0x00000004;
31     const CHECK_MODE_APPLY_DEFAULTS =   0x00000008;
32     const CHECK_MODE_EXCEPTIONS =       0x00000010;
33     const CHECK_MODE_DISABLE_FORMAT =   0x00000020;
34     const CHECK_MODE_ONLY_REQUIRED_DEFAULTS   = 0x00000080;
35     const CHECK_MODE_VALIDATE_SCHEMA =  0x00000100;
36
37     /**
38      * Bubble down the path
39      *
40      * @param JsonPointer|null $path Current path
41      * @param mixed            $i    What to append to the path
42      *
43      * @return JsonPointer;
44      */
45     protected function incrementPath(JsonPointer $path = null, $i)
46     {
47         $path = $path ?: new JsonPointer('');
48         $path = $path->withPropertyPaths(
49             array_merge(
50                 $path->getPropertyPaths(),
51                 array_filter(array($i), 'strlen')
52             )
53         );
54
55         return $path;
56     }
57
58     /**
59      * Validates an array
60      *
61      * @param mixed            $value
62      * @param mixed            $schema
63      * @param JsonPointer|null $path
64      * @param mixed            $i
65      */
66     protected function checkArray(&$value, $schema = null, JsonPointer $path = null, $i = null)
67     {
68         $validator = $this->factory->createInstanceFor('collection');
69         $validator->check($value, $schema, $path, $i);
70
71         $this->addErrors($validator->getErrors());
72     }
73
74     /**
75      * Validates an object
76      *
77      * @param mixed            $value
78      * @param mixed            $schema
79      * @param JsonPointer|null $path
80      * @param mixed            $properties
81      * @param mixed            $additionalProperties
82      * @param mixed            $patternProperties
83      */
84     protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $properties = null,
85         $additionalProperties = null, $patternProperties = null, $appliedDefaults = array())
86     {
87         $validator = $this->factory->createInstanceFor('object');
88         $validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults);
89
90         $this->addErrors($validator->getErrors());
91     }
92
93     /**
94      * Validates the type of a property
95      *
96      * @param mixed            $value
97      * @param mixed            $schema
98      * @param JsonPointer|null $path
99      * @param mixed            $i
100      */
101     protected function checkType(&$value, $schema = null, JsonPointer $path = null, $i = null)
102     {
103         $validator = $this->factory->createInstanceFor('type');
104         $validator->check($value, $schema, $path, $i);
105
106         $this->addErrors($validator->getErrors());
107     }
108
109     /**
110      * Checks a undefined element
111      *
112      * @param mixed            $value
113      * @param mixed            $schema
114      * @param JsonPointer|null $path
115      * @param mixed            $i
116      */
117     protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $fromDefault = false)
118     {
119         $validator = $this->factory->createInstanceFor('undefined');
120
121         $validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i, $fromDefault);
122
123         $this->addErrors($validator->getErrors());
124     }
125
126     /**
127      * Checks a string element
128      *
129      * @param mixed            $value
130      * @param mixed            $schema
131      * @param JsonPointer|null $path
132      * @param mixed            $i
133      */
134     protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
135     {
136         $validator = $this->factory->createInstanceFor('string');
137         $validator->check($value, $schema, $path, $i);
138
139         $this->addErrors($validator->getErrors());
140     }
141
142     /**
143      * Checks a number element
144      *
145      * @param mixed       $value
146      * @param mixed       $schema
147      * @param JsonPointer $path
148      * @param mixed       $i
149      */
150     protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
151     {
152         $validator = $this->factory->createInstanceFor('number');
153         $validator->check($value, $schema, $path, $i);
154
155         $this->addErrors($validator->getErrors());
156     }
157
158     /**
159      * Checks a enum element
160      *
161      * @param mixed            $value
162      * @param mixed            $schema
163      * @param JsonPointer|null $path
164      * @param mixed            $i
165      */
166     protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
167     {
168         $validator = $this->factory->createInstanceFor('enum');
169         $validator->check($value, $schema, $path, $i);
170
171         $this->addErrors($validator->getErrors());
172     }
173
174     /**
175      * Checks format of an element
176      *
177      * @param mixed            $value
178      * @param mixed            $schema
179      * @param JsonPointer|null $path
180      * @param mixed            $i
181      */
182     protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
183     {
184         $validator = $this->factory->createInstanceFor('format');
185         $validator->check($value, $schema, $path, $i);
186
187         $this->addErrors($validator->getErrors());
188     }
189
190     /**
191      * Get the type check based on the set check mode.
192      *
193      * @return TypeCheck\TypeCheckInterface
194      */
195     protected function getTypeCheck()
196     {
197         return $this->factory->getTypeCheck();
198     }
199
200     /**
201      * @param JsonPointer $pointer
202      *
203      * @return string property path
204      */
205     protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
206     {
207         $result = array_map(
208             function ($path) {
209                 return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
210             },
211             $pointer->getPropertyPaths()
212         );
213
214         return trim(implode('', $result), '.');
215     }
216 }