secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Constraints
/
EnumConstraint.php
secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Constraints
Last commit date
TypeCheck
8 months ago
BaseConstraint.php
8 months ago
CollectionConstraint.php
8 months ago
Constraint.php
8 months ago
ConstraintInterface.php
8 months ago
EnumConstraint.php
8 months ago
Factory.php
8 months ago
FormatConstraint.php
8 months ago
NumberConstraint.php
8 months ago
ObjectConstraint.php
8 months ago
SchemaConstraint.php
8 months ago
StringConstraint.php
8 months ago
TypeConstraint.php
8 months ago
UndefinedConstraint.php
8 months ago
EnumConstraint.php
55 lines
| 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 EnumConstraint Constraints, validates an element against a given set of possibilities |
| 16 | * |
| 17 | * @author Robert Schönthal <seroscho@googlemail.com> |
| 18 | * @author Bruno Prieto Reis <bruno.p.reis@gmail.com> |
| 19 | */ |
| 20 | class EnumConstraint extends Constraint |
| 21 | { |
| 22 | /** |
| 23 | * {@inheritdoc} |
| 24 | */ |
| 25 | public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) |
| 26 | { |
| 27 | // Only validate enum if the attribute exists |
| 28 | if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) { |
| 29 | return; |
| 30 | } |
| 31 | $type = gettype($element); |
| 32 | |
| 33 | foreach ($schema->enum as $enum) { |
| 34 | $enumType = gettype($enum); |
| 35 | if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') { |
| 36 | if ((object) $element == $enum) { |
| 37 | return; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if ($type === gettype($enum)) { |
| 42 | if ($type == 'object') { |
| 43 | if ($element == $enum) { |
| 44 | return; |
| 45 | } |
| 46 | } elseif ($element === $enum) { |
| 47 | return; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | $this->addError($path, 'Does not have a value in the enumeration ' . json_encode($schema->enum), 'enum', array('enum' => $schema->enum)); |
| 53 | } |
| 54 | } |
| 55 |