4 * This file is part of the JsonSchema package.
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
10 namespace JsonSchema\Constraints;
12 use JsonSchema\Entity\JsonPointer;
13 use JsonSchema\SchemaStorage;
14 use JsonSchema\Uri\UriRetriever;
15 use JsonSchema\UriRetrieverInterface;
18 * The Base Constraints, all Validators should extend this class
20 * @author Robert Schönthal <seroscho@googlemail.com>
21 * @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
23 abstract class Constraint extends BaseConstraint implements ConstraintInterface
25 protected $inlineSchemaProperty = '$schema';
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;
38 * Bubble down the path
40 * @param JsonPointer|null $path Current path
41 * @param mixed $i What to append to the path
43 * @return JsonPointer;
45 protected function incrementPath(JsonPointer $path = null, $i)
47 $path = $path ?: new JsonPointer('');
48 $path = $path->withPropertyPaths(
50 $path->getPropertyPaths(),
51 array_filter(array($i), 'strlen')
62 * @param mixed $schema
63 * @param JsonPointer|null $path
66 protected function checkArray(&$value, $schema = null, JsonPointer $path = null, $i = null)
68 $validator = $this->factory->createInstanceFor('collection');
69 $validator->check($value, $schema, $path, $i);
71 $this->addErrors($validator->getErrors());
78 * @param mixed $schema
79 * @param JsonPointer|null $path
80 * @param mixed $properties
81 * @param mixed $additionalProperties
82 * @param mixed $patternProperties
84 protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $properties = null,
85 $additionalProperties = null, $patternProperties = null, $appliedDefaults = array())
87 $validator = $this->factory->createInstanceFor('object');
88 $validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults);
90 $this->addErrors($validator->getErrors());
94 * Validates the type of a property
97 * @param mixed $schema
98 * @param JsonPointer|null $path
101 protected function checkType(&$value, $schema = null, JsonPointer $path = null, $i = null)
103 $validator = $this->factory->createInstanceFor('type');
104 $validator->check($value, $schema, $path, $i);
106 $this->addErrors($validator->getErrors());
110 * Checks a undefined element
112 * @param mixed $value
113 * @param mixed $schema
114 * @param JsonPointer|null $path
117 protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $fromDefault = false)
119 $validator = $this->factory->createInstanceFor('undefined');
121 $validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i, $fromDefault);
123 $this->addErrors($validator->getErrors());
127 * Checks a string element
129 * @param mixed $value
130 * @param mixed $schema
131 * @param JsonPointer|null $path
134 protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
136 $validator = $this->factory->createInstanceFor('string');
137 $validator->check($value, $schema, $path, $i);
139 $this->addErrors($validator->getErrors());
143 * Checks a number element
145 * @param mixed $value
146 * @param mixed $schema
147 * @param JsonPointer $path
150 protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
152 $validator = $this->factory->createInstanceFor('number');
153 $validator->check($value, $schema, $path, $i);
155 $this->addErrors($validator->getErrors());
159 * Checks a enum element
161 * @param mixed $value
162 * @param mixed $schema
163 * @param JsonPointer|null $path
166 protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
168 $validator = $this->factory->createInstanceFor('enum');
169 $validator->check($value, $schema, $path, $i);
171 $this->addErrors($validator->getErrors());
175 * Checks format of an element
177 * @param mixed $value
178 * @param mixed $schema
179 * @param JsonPointer|null $path
182 protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
184 $validator = $this->factory->createInstanceFor('format');
185 $validator->check($value, $schema, $path, $i);
187 $this->addErrors($validator->getErrors());
191 * Get the type check based on the set check mode.
193 * @return TypeCheck\TypeCheckInterface
195 protected function getTypeCheck()
197 return $this->factory->getTypeCheck();
201 * @param JsonPointer $pointer
203 * @return string property path
205 protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
209 return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
211 $pointer->getPropertyPaths()
214 return trim(implode('', $result), '.');