* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * ChoiceValidator validates that the value is one of the expected values. * * @author Fabien Potencier * @author Florian Eckerstorfer * @author Bernhard Schussek */ 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->getObject(), $constraint->callback)) && !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 (true !== $constraint->strict) { @trigger_error('Not setting the strict option of the Choice constraint to true is deprecated since Symfony 3.4 and will throw an exception in 4.0.', E_USER_DEPRECATED); } if ($constraint->multiple) { foreach ($value as $_value) { if (!in_array($_value, $choices, $constraint->strict)) { $this->context->buildViolation($constraint->multipleMessage) ->setParameter('{{ value }}', $this->formatValue($_value)) ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->setInvalidValue($_value) ->addViolation(); return; } } $count = count($value); if (null !== $constraint->min && $count < $constraint->min) { $this->context->buildViolation($constraint->minMessage) ->setParameter('{{ limit }}', $constraint->min) ->setPlural((int) $constraint->min) ->setCode(Choice::TOO_FEW_ERROR) ->addViolation(); return; } if (null !== $constraint->max && $count > $constraint->max) { $this->context->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)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->addViolation(); } } } __halt_compiler();----SIGNATURE:----d+lI0KAl7Dg2kCrZjMfG7i9zBmoWtd5N/7PzMEcL2KdorY5fhmQJWCH/xNE/E9Ru80W9+3s4fCDE45cuksWNPEw4ZFVcSufUEkRmh3HD0WZmiD46/gYYdRaiTvdrefVMiuRdJBigJjcMOi5MLLJoJVL1hpBXih9uh1Xp8OIlsvnGEP5Io9AJ06Pe8voOR3F9NM6tGk705QAxCmjVP6LD1JYjb7GxXz4T0wxCBzFKM2uVsdorvpJ/mHOdotFlogmtffkhTuvJAJMOe4ddKUyS3bsIjWk8qo8sePcggoRgZg8leDl8XVgIfzu/RVp2z6GiRWobK+cltmHHu83SXo4zTMayAuc3hw/qDq9ssn53h2aRSCvHfOlPX6zcSO3Q8wrYh8UXLhI/eu3u4LmhdAJNpyPxIazGdAJGSrya2CoiIePJpklcmrI/XPTTIE4zsAnOwn9+ka800l4cqYNOgyQJgy69cqm2ERe14eQG3d8wh06WbgKI60bVNY/XqRO6BwkHomvU6MJAb9n097MpMxwc4PH2XhULjuov9BUG0MF4OOzeVAUooOfkZ0MaBsPBbOOA7PP/R0w4D6Yl5kVig18IT2d2DD4mHjOZQ0lNn1TGIfVJfUTg+pAS2KfCrmwA7hhPj6c4tzEkTntSYwzZPRtNkTjkhPuJ9t4lJ3zwNWPOR8A=----ATTACHMENT:----NjMwNzgyODYzMzUwNzE4OCAzNDMyNTc0Nzk3NzA3MTY0IDM4MTY0NTMzMzM4OTE4NjE=