Sha256: bb230a041ab19eb9561b99ad5529c05ee57bcf6c5268c04146c777623996975b

Contents?: true

Size: 1.61 KB

Versions: 42

Compression:

Stored size: 1.61 KB

Contents

<?php

/*
 * This file is part of the JsonSchema package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;

/**
 * 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>
 */
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));
    }
}

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
dependabot-composer-0.119.0.beta1 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.113.19 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.112.1 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.111.57 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.111.56 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.111.52 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.111.50 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.111.25 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.111.17 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.111.15 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.110.13 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.108.11 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.108.8 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.107.47 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.107.39 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.107.36 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.107.28 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.107.14 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.106.12 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php
dependabot-composer-0.106.11 helpers/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php