Debian Codegraph @

vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php

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