ex
Fork of mbed-os-example-mbed5-blinky by
dumi_doc-master/schema/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/TypeConstraint.php@44:c1d8923072ba, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:27:22 2017 +0800
- Revision:
- 44:c1d8923072ba
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 44:c1d8923072ba | 1 | <?php |
| TMBOY | 44:c1d8923072ba | 2 | |
| TMBOY | 44:c1d8923072ba | 3 | /* |
| TMBOY | 44:c1d8923072ba | 4 | * This file is part of the JsonSchema package. |
| TMBOY | 44:c1d8923072ba | 5 | * |
| TMBOY | 44:c1d8923072ba | 6 | * For the full copyright and license information, please view the LICENSE |
| TMBOY | 44:c1d8923072ba | 7 | * file that was distributed with this source code. |
| TMBOY | 44:c1d8923072ba | 8 | */ |
| TMBOY | 44:c1d8923072ba | 9 | |
| TMBOY | 44:c1d8923072ba | 10 | namespace JsonSchema\Constraints; |
| TMBOY | 44:c1d8923072ba | 11 | |
| TMBOY | 44:c1d8923072ba | 12 | use JsonSchema\Exception\InvalidArgumentException; |
| TMBOY | 44:c1d8923072ba | 13 | use JsonSchema\Entity\JsonPointer; |
| TMBOY | 44:c1d8923072ba | 14 | use UnexpectedValueException as StandardUnexpectedValueException; |
| TMBOY | 44:c1d8923072ba | 15 | |
| TMBOY | 44:c1d8923072ba | 16 | /** |
| TMBOY | 44:c1d8923072ba | 17 | * The TypeConstraint Constraints, validates an element against a given type |
| TMBOY | 44:c1d8923072ba | 18 | * |
| TMBOY | 44:c1d8923072ba | 19 | * @author Robert Schönthal <seroscho@googlemail.com> |
| TMBOY | 44:c1d8923072ba | 20 | * @author Bruno Prieto Reis <bruno.p.reis@gmail.com> |
| TMBOY | 44:c1d8923072ba | 21 | */ |
| TMBOY | 44:c1d8923072ba | 22 | class TypeConstraint extends Constraint |
| TMBOY | 44:c1d8923072ba | 23 | { |
| TMBOY | 44:c1d8923072ba | 24 | /** |
| TMBOY | 44:c1d8923072ba | 25 | * @var array|string[] type wordings for validation error messages |
| TMBOY | 44:c1d8923072ba | 26 | */ |
| TMBOY | 44:c1d8923072ba | 27 | static $wording = array( |
| TMBOY | 44:c1d8923072ba | 28 | 'integer' => 'an integer', |
| TMBOY | 44:c1d8923072ba | 29 | 'number' => 'a number', |
| TMBOY | 44:c1d8923072ba | 30 | 'boolean' => 'a boolean', |
| TMBOY | 44:c1d8923072ba | 31 | 'object' => 'an object', |
| TMBOY | 44:c1d8923072ba | 32 | 'array' => 'an array', |
| TMBOY | 44:c1d8923072ba | 33 | 'string' => 'a string', |
| TMBOY | 44:c1d8923072ba | 34 | 'null' => 'a null', |
| TMBOY | 44:c1d8923072ba | 35 | 'any' => NULL, // validation of 'any' is always true so is not needed in message wording |
| TMBOY | 44:c1d8923072ba | 36 | 0 => NULL, // validation of a false-y value is always true, so not needed as well |
| TMBOY | 44:c1d8923072ba | 37 | ); |
| TMBOY | 44:c1d8923072ba | 38 | |
| TMBOY | 44:c1d8923072ba | 39 | /** |
| TMBOY | 44:c1d8923072ba | 40 | * {@inheritDoc} |
| TMBOY | 44:c1d8923072ba | 41 | */ |
| TMBOY | 44:c1d8923072ba | 42 | public function check($value = null, $schema = null, JsonPointer $path = null, $i = null) |
| TMBOY | 44:c1d8923072ba | 43 | { |
| TMBOY | 44:c1d8923072ba | 44 | $type = isset($schema->type) ? $schema->type : null; |
| TMBOY | 44:c1d8923072ba | 45 | $isValid = false; |
| TMBOY | 44:c1d8923072ba | 46 | $wording = array(); |
| TMBOY | 44:c1d8923072ba | 47 | |
| TMBOY | 44:c1d8923072ba | 48 | if (is_array($type)) { |
| TMBOY | 44:c1d8923072ba | 49 | $this->validateTypesArray($value, $type, $wording, $isValid, $path); |
| TMBOY | 44:c1d8923072ba | 50 | } elseif (is_object($type)) { |
| TMBOY | 44:c1d8923072ba | 51 | $this->checkUndefined($value, $type, $path); |
| TMBOY | 44:c1d8923072ba | 52 | return; |
| TMBOY | 44:c1d8923072ba | 53 | } else { |
| TMBOY | 44:c1d8923072ba | 54 | $isValid = $this->validateType($value, $type); |
| TMBOY | 44:c1d8923072ba | 55 | } |
| TMBOY | 44:c1d8923072ba | 56 | |
| TMBOY | 44:c1d8923072ba | 57 | if ($isValid === false) { |
| TMBOY | 44:c1d8923072ba | 58 | if (!is_array($type)) { |
| TMBOY | 44:c1d8923072ba | 59 | $this->validateTypeNameWording($type); |
| TMBOY | 44:c1d8923072ba | 60 | $wording[] = self::$wording[$type]; |
| TMBOY | 44:c1d8923072ba | 61 | } |
| TMBOY | 44:c1d8923072ba | 62 | $this->addError($path, ucwords(gettype($value)) . " value found, but " . |
| TMBOY | 44:c1d8923072ba | 63 | $this->implodeWith($wording, ', ', 'or') . " is required", 'type'); |
| TMBOY | 44:c1d8923072ba | 64 | } |
| TMBOY | 44:c1d8923072ba | 65 | } |
| TMBOY | 44:c1d8923072ba | 66 | |
| TMBOY | 44:c1d8923072ba | 67 | /** |
| TMBOY | 44:c1d8923072ba | 68 | * Validates the given $value against the array of types in $type. Sets the value |
| TMBOY | 44:c1d8923072ba | 69 | * of $isValid to true, if at least one $type mateches the type of $value or the value |
| TMBOY | 44:c1d8923072ba | 70 | * passed as $isValid is already true. |
| TMBOY | 44:c1d8923072ba | 71 | * |
| TMBOY | 44:c1d8923072ba | 72 | * @param mixed $value Value to validate |
| TMBOY | 44:c1d8923072ba | 73 | * @param array $type TypeConstraints to check agains |
| TMBOY | 44:c1d8923072ba | 74 | * @param array $validTypesWording An array of wordings of the valid types of the array $type |
| TMBOY | 44:c1d8923072ba | 75 | * @param boolean $isValid The current validation value |
| TMBOY | 44:c1d8923072ba | 76 | * @param $path |
| TMBOY | 44:c1d8923072ba | 77 | */ |
| TMBOY | 44:c1d8923072ba | 78 | protected function validateTypesArray($value, array $type, &$validTypesWording, &$isValid, |
| TMBOY | 44:c1d8923072ba | 79 | $path) { |
| TMBOY | 44:c1d8923072ba | 80 | foreach ($type as $tp) { |
| TMBOY | 44:c1d8923072ba | 81 | // $tp can be an object, if it's a schema instead of a simple type, validate it |
| TMBOY | 44:c1d8923072ba | 82 | // with a new type constraint |
| TMBOY | 44:c1d8923072ba | 83 | if (is_object($tp)) { |
| TMBOY | 44:c1d8923072ba | 84 | if (!$isValid) { |
| TMBOY | 44:c1d8923072ba | 85 | $validator = $this->factory->createInstanceFor('type'); |
| TMBOY | 44:c1d8923072ba | 86 | $subSchema = new \stdClass(); |
| TMBOY | 44:c1d8923072ba | 87 | $subSchema->type = $tp; |
| TMBOY | 44:c1d8923072ba | 88 | $validator->check($value, $subSchema, $path, null); |
| TMBOY | 44:c1d8923072ba | 89 | $error = $validator->getErrors(); |
| TMBOY | 44:c1d8923072ba | 90 | $isValid = !(bool)$error; |
| TMBOY | 44:c1d8923072ba | 91 | $validTypesWording[] = self::$wording['object']; |
| TMBOY | 44:c1d8923072ba | 92 | } |
| TMBOY | 44:c1d8923072ba | 93 | } else { |
| TMBOY | 44:c1d8923072ba | 94 | $this->validateTypeNameWording( $tp ); |
| TMBOY | 44:c1d8923072ba | 95 | $validTypesWording[] = self::$wording[$tp]; |
| TMBOY | 44:c1d8923072ba | 96 | if (!$isValid) { |
| TMBOY | 44:c1d8923072ba | 97 | $isValid = $this->validateType( $value, $tp ); |
| TMBOY | 44:c1d8923072ba | 98 | } |
| TMBOY | 44:c1d8923072ba | 99 | } |
| TMBOY | 44:c1d8923072ba | 100 | } |
| TMBOY | 44:c1d8923072ba | 101 | } |
| TMBOY | 44:c1d8923072ba | 102 | |
| TMBOY | 44:c1d8923072ba | 103 | /** |
| TMBOY | 44:c1d8923072ba | 104 | * Implodes the given array like implode() with turned around parameters and with the |
| TMBOY | 44:c1d8923072ba | 105 | * difference, that, if $listEnd isn't false, the last element delimiter is $listEnd instead of |
| TMBOY | 44:c1d8923072ba | 106 | * $delimiter. |
| TMBOY | 44:c1d8923072ba | 107 | * |
| TMBOY | 44:c1d8923072ba | 108 | * @param array $elements The elements to implode |
| TMBOY | 44:c1d8923072ba | 109 | * @param string $delimiter The delimiter to use |
| TMBOY | 44:c1d8923072ba | 110 | * @param bool $listEnd The last delimiter to use (defaults to $delimiter) |
| TMBOY | 44:c1d8923072ba | 111 | * @return string |
| TMBOY | 44:c1d8923072ba | 112 | */ |
| TMBOY | 44:c1d8923072ba | 113 | protected function implodeWith(array $elements, $delimiter = ', ', $listEnd = false) { |
| TMBOY | 44:c1d8923072ba | 114 | if ($listEnd === false || !isset($elements[1])) { |
| TMBOY | 44:c1d8923072ba | 115 | return implode($delimiter, $elements); |
| TMBOY | 44:c1d8923072ba | 116 | } |
| TMBOY | 44:c1d8923072ba | 117 | $lastElement = array_slice($elements, -1); |
| TMBOY | 44:c1d8923072ba | 118 | $firsElements = join($delimiter, array_slice($elements, 0, -1)); |
| TMBOY | 44:c1d8923072ba | 119 | $implodedElements = array_merge(array($firsElements), $lastElement); |
| TMBOY | 44:c1d8923072ba | 120 | return join(" $listEnd ", $implodedElements); |
| TMBOY | 44:c1d8923072ba | 121 | } |
| TMBOY | 44:c1d8923072ba | 122 | |
| TMBOY | 44:c1d8923072ba | 123 | /** |
| TMBOY | 44:c1d8923072ba | 124 | * Validates the given $type, if there's an associated self::$wording. If not, throws an |
| TMBOY | 44:c1d8923072ba | 125 | * exception. |
| TMBOY | 44:c1d8923072ba | 126 | * |
| TMBOY | 44:c1d8923072ba | 127 | * @param string $type The type to validate |
| TMBOY | 44:c1d8923072ba | 128 | * |
| TMBOY | 44:c1d8923072ba | 129 | * @throws StandardUnexpectedValueException |
| TMBOY | 44:c1d8923072ba | 130 | */ |
| TMBOY | 44:c1d8923072ba | 131 | protected function validateTypeNameWording( $type) { |
| TMBOY | 44:c1d8923072ba | 132 | if (!isset(self::$wording[$type])) { |
| TMBOY | 44:c1d8923072ba | 133 | throw new StandardUnexpectedValueException( |
| TMBOY | 44:c1d8923072ba | 134 | sprintf( |
| TMBOY | 44:c1d8923072ba | 135 | "No wording for %s available, expected wordings are: [%s]", |
| TMBOY | 44:c1d8923072ba | 136 | var_export($type, true), |
| TMBOY | 44:c1d8923072ba | 137 | implode(', ', array_filter(self::$wording))) |
| TMBOY | 44:c1d8923072ba | 138 | ); |
| TMBOY | 44:c1d8923072ba | 139 | } |
| TMBOY | 44:c1d8923072ba | 140 | } |
| TMBOY | 44:c1d8923072ba | 141 | |
| TMBOY | 44:c1d8923072ba | 142 | /** |
| TMBOY | 44:c1d8923072ba | 143 | * Verifies that a given value is of a certain type |
| TMBOY | 44:c1d8923072ba | 144 | * |
| TMBOY | 44:c1d8923072ba | 145 | * @param mixed $value Value to validate |
| TMBOY | 44:c1d8923072ba | 146 | * @param string $type TypeConstraint to check against |
| TMBOY | 44:c1d8923072ba | 147 | * |
| TMBOY | 44:c1d8923072ba | 148 | * @return boolean |
| TMBOY | 44:c1d8923072ba | 149 | * |
| TMBOY | 44:c1d8923072ba | 150 | * @throws InvalidArgumentException |
| TMBOY | 44:c1d8923072ba | 151 | */ |
| TMBOY | 44:c1d8923072ba | 152 | protected function validateType($value, $type) |
| TMBOY | 44:c1d8923072ba | 153 | { |
| TMBOY | 44:c1d8923072ba | 154 | //mostly the case for inline schema |
| TMBOY | 44:c1d8923072ba | 155 | if (!$type) { |
| TMBOY | 44:c1d8923072ba | 156 | return true; |
| TMBOY | 44:c1d8923072ba | 157 | } |
| TMBOY | 44:c1d8923072ba | 158 | |
| TMBOY | 44:c1d8923072ba | 159 | if ('integer' === $type) { |
| TMBOY | 44:c1d8923072ba | 160 | return is_int($value); |
| TMBOY | 44:c1d8923072ba | 161 | } |
| TMBOY | 44:c1d8923072ba | 162 | |
| TMBOY | 44:c1d8923072ba | 163 | if ('number' === $type) { |
| TMBOY | 44:c1d8923072ba | 164 | return is_numeric($value) && !is_string($value); |
| TMBOY | 44:c1d8923072ba | 165 | } |
| TMBOY | 44:c1d8923072ba | 166 | |
| TMBOY | 44:c1d8923072ba | 167 | if ('boolean' === $type) { |
| TMBOY | 44:c1d8923072ba | 168 | return is_bool($value); |
| TMBOY | 44:c1d8923072ba | 169 | } |
| TMBOY | 44:c1d8923072ba | 170 | |
| TMBOY | 44:c1d8923072ba | 171 | if ('object' === $type) { |
| TMBOY | 44:c1d8923072ba | 172 | return $this->getTypeCheck()->isObject($value); |
| TMBOY | 44:c1d8923072ba | 173 | } |
| TMBOY | 44:c1d8923072ba | 174 | |
| TMBOY | 44:c1d8923072ba | 175 | if ('array' === $type) { |
| TMBOY | 44:c1d8923072ba | 176 | return $this->getTypeCheck()->isArray($value); |
| TMBOY | 44:c1d8923072ba | 177 | } |
| TMBOY | 44:c1d8923072ba | 178 | |
| TMBOY | 44:c1d8923072ba | 179 | if ('string' === $type) { |
| TMBOY | 44:c1d8923072ba | 180 | return is_string($value); |
| TMBOY | 44:c1d8923072ba | 181 | } |
| TMBOY | 44:c1d8923072ba | 182 | |
| TMBOY | 44:c1d8923072ba | 183 | if ('email' === $type) { |
| TMBOY | 44:c1d8923072ba | 184 | return is_string($value); |
| TMBOY | 44:c1d8923072ba | 185 | } |
| TMBOY | 44:c1d8923072ba | 186 | |
| TMBOY | 44:c1d8923072ba | 187 | if ('null' === $type) { |
| TMBOY | 44:c1d8923072ba | 188 | return is_null($value); |
| TMBOY | 44:c1d8923072ba | 189 | } |
| TMBOY | 44:c1d8923072ba | 190 | |
| TMBOY | 44:c1d8923072ba | 191 | if ('any' === $type) { |
| TMBOY | 44:c1d8923072ba | 192 | return true; |
| TMBOY | 44:c1d8923072ba | 193 | } |
| TMBOY | 44:c1d8923072ba | 194 | |
| TMBOY | 44:c1d8923072ba | 195 | throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type); |
| TMBOY | 44:c1d8923072ba | 196 | } |
| TMBOY | 44:c1d8923072ba | 197 | } |
