You are here

class ChoiceValidator in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/ChoiceValidator.php \Symfony\Component\Validator\Constraints\ChoiceValidator

ChoiceValidator validates that the value is one of the expected values.

@author Fabien Potencier <fabien@symfony.com> @author Florian Eckerstorfer <florian@eckerstorfer.org> @author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

Expanded class hierarchy of ChoiceValidator

2 files declare their use of ChoiceValidator
AllowedValuesConstraintValidator.php in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraintValidator.php
Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraintValidator.
ChoiceValidatorTest.php in vendor/symfony/validator/Tests/Constraints/ChoiceValidatorTest.php

File

vendor/symfony/validator/Constraints/ChoiceValidator.php, line 27

Namespace

Symfony\Component\Validator\Constraints
View source
class ChoiceValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if (!$constraint instanceof Choice) {
      throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Choice');
    }
    if (!is_array($constraint->choices) && !$constraint->callback) {
      throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
    }
    if (null === $value) {
      return;
    }
    if ($constraint->multiple && !is_array($value)) {
      throw new UnexpectedTypeException($value, 'array');
    }
    if ($constraint->callback) {
      if (!is_callable($choices = array(
        $this->context
          ->getClassName(),
        $constraint->callback,
      )) && !is_callable($choices = $constraint->callback)) {
        throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
      }
      $choices = call_user_func($choices);
    }
    else {
      $choices = $constraint->choices;
    }
    if ($constraint->multiple) {
      foreach ($value as $_value) {
        if (!in_array($_value, $choices, $constraint->strict)) {
          if ($this->context instanceof ExecutionContextInterface) {
            $this->context
              ->buildViolation($constraint->multipleMessage)
              ->setParameter('{{ value }}', $this
              ->formatValue($_value))
              ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
              ->setInvalidValue($_value)
              ->addViolation();
          }
          else {
            $this
              ->buildViolation($constraint->multipleMessage)
              ->setParameter('{{ value }}', $this
              ->formatValue($_value))
              ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
              ->setInvalidValue($_value)
              ->addViolation();
          }
          return;
        }
      }
      $count = count($value);
      if ($constraint->min !== null && $count < $constraint->min) {
        if ($this->context instanceof ExecutionContextInterface) {
          $this->context
            ->buildViolation($constraint->minMessage)
            ->setParameter('{{ limit }}', $constraint->min)
            ->setPlural((int) $constraint->min)
            ->setCode(Choice::TOO_FEW_ERROR)
            ->addViolation();
        }
        else {
          $this
            ->buildViolation($constraint->minMessage)
            ->setParameter('{{ limit }}', $constraint->min)
            ->setPlural((int) $constraint->min)
            ->setCode(Choice::TOO_FEW_ERROR)
            ->addViolation();
        }
        return;
      }
      if ($constraint->max !== null && $count > $constraint->max) {
        if ($this->context instanceof ExecutionContextInterface) {
          $this->context
            ->buildViolation($constraint->maxMessage)
            ->setParameter('{{ limit }}', $constraint->max)
            ->setPlural((int) $constraint->max)
            ->setCode(Choice::TOO_MANY_ERROR)
            ->addViolation();
        }
        else {
          $this
            ->buildViolation($constraint->maxMessage)
            ->setParameter('{{ limit }}', $constraint->max)
            ->setPlural((int) $constraint->max)
            ->setCode(Choice::TOO_MANY_ERROR)
            ->addViolation();
        }
        return;
      }
    }
    elseif (!in_array($value, $choices, $constraint->strict)) {
      if ($this->context instanceof ExecutionContextInterface) {
        $this->context
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
          ->addViolation();
      }
      else {
        $this
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
          ->addViolation();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChoiceValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate 1
ConstraintValidator::$context protected property 3
ConstraintValidator::buildViolation Deprecated protected function Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API.
ConstraintValidator::buildViolationInContext Deprecated protected function Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API.
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize 1
ConstraintValidator::OBJECT_TO_STRING constant Whether to cast objects with a "__toString()" method to strings.
ConstraintValidator::PRETTY_DATE constant Whether to format {@link \DateTime} objects as RFC-3339 dates ("Y-m-d H:i:s").