| 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 <[email protected]> |
| 18 |
* @author Bruno Prieto Reis <[email protected]> |
| 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 |
} |