Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. EnumConstraint.php

class EnumConstraint

The EnumConstraint Constraints, validates an element against a given set of possibilities

@author Robert Schönthal <seroscho@googlemail.com> @author Bruno Prieto Reis <bruno.p.reis@gmail.com>

Hierarchy

  • class \JsonSchema\Constraints\BaseConstraint
    • class \JsonSchema\Constraints\Constraint extends \JsonSchema\Constraints\BaseConstraint implements \JsonSchema\Constraints\ConstraintInterface
      • class \JsonSchema\Constraints\EnumConstraint extends \JsonSchema\Constraints\Constraint

Expanded class hierarchy of EnumConstraint

File

vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php, line 20

Namespace

JsonSchema\Constraints
View source
class EnumConstraint extends Constraint {
    
    /**
     * {@inheritdoc}
     */
    public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) {
        // Only validate enum if the attribute exists
        if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {
            return;
        }
        $type = gettype($element);
        foreach ($schema->enum as $enum) {
            $enumType = gettype($enum);
            if ($this->factory
                ->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') {
                if ((object) $element == $enum) {
                    return;
                }
            }
            if ($type === gettype($enum)) {
                if ($type == 'object') {
                    if ($element == $enum) {
                        return;
                    }
                }
                elseif ($element === $enum) {
                    return;
                }
            }
        }
        $this->addError($path, 'Does not have a value in the enumeration ' . json_encode($schema->enum), 'enum', array(
            'enum' => $schema->enum,
        ));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BaseConstraint::$errorMask protected property
BaseConstraint::$errors protected property
BaseConstraint::$factory protected property
BaseConstraint::addError public function
BaseConstraint::addErrors public function
BaseConstraint::arrayToObjectRecursive public static function Recursively cast an associative array to an object
BaseConstraint::getErrorMask public function Get the error mask
BaseConstraint::getErrors public function
BaseConstraint::isValid public function
BaseConstraint::numErrors public function
BaseConstraint::reset public function Clears any reported errors. Should be used between
multiple validation checks.
BaseConstraint::__construct public function
Constraint::$inlineSchemaProperty protected property
Constraint::checkArray protected function Validates an array
Constraint::checkEnum protected function Checks a enum element
Constraint::checkFormat protected function Checks format of an element
Constraint::checkNumber protected function Checks a number element
Constraint::checkObject protected function Validates an object
Constraint::checkString protected function Checks a string element
Constraint::checkType protected function Validates the type of a property
Constraint::checkUndefined protected function Checks a undefined element
Constraint::CHECK_MODE_APPLY_DEFAULTS constant
Constraint::CHECK_MODE_COERCE_TYPES constant
Constraint::CHECK_MODE_DISABLE_FORMAT constant
Constraint::CHECK_MODE_EXCEPTIONS constant
Constraint::CHECK_MODE_NONE constant
Constraint::CHECK_MODE_NORMAL constant
Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS constant
Constraint::CHECK_MODE_TYPE_CAST constant
Constraint::CHECK_MODE_VALIDATE_SCHEMA constant
Constraint::convertJsonPointerIntoPropertyPath protected function
Constraint::getTypeCheck protected function Get the type check based on the set check mode.
Constraint::incrementPath protected function Bubble down the path
EnumConstraint::check public function invokes the validation of an element Overrides ConstraintInterface::check

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal