Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Constraints; Chris@0: Chris@0: use Symfony\Component\Validator\Constraint; Chris@0: use Symfony\Component\Validator\ConstraintValidator; Chris@0: use Symfony\Component\Validator\Exception\ConstraintDefinitionException; Chris@0: use Symfony\Component\Validator\Exception\UnexpectedTypeException; Chris@0: Chris@0: /** Chris@0: * ChoiceValidator validates that the value is one of the expected values. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Florian Eckerstorfer Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class ChoiceValidator extends ConstraintValidator Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validate($value, Constraint $constraint) Chris@0: { Chris@0: if (!$constraint instanceof Choice) { Chris@0: throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice'); Chris@0: } Chris@0: Chris@17: if (!\is_array($constraint->choices) && !$constraint->callback) { Chris@0: throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice'); Chris@0: } Chris@0: Chris@0: if (null === $value) { Chris@0: return; Chris@0: } Chris@0: Chris@17: if ($constraint->multiple && !\is_array($value)) { Chris@0: throw new UnexpectedTypeException($value, 'array'); Chris@0: } Chris@0: Chris@0: if ($constraint->callback) { Chris@17: if (!\is_callable($choices = [$this->context->getObject(), $constraint->callback]) Chris@17: && !\is_callable($choices = [$this->context->getClassName(), $constraint->callback]) Chris@17: && !\is_callable($choices = $constraint->callback) Chris@0: ) { Chris@0: throw new ConstraintDefinitionException('The Choice constraint expects a valid callback'); Chris@0: } Chris@17: $choices = \call_user_func($choices); Chris@0: } else { Chris@0: $choices = $constraint->choices; Chris@0: } Chris@0: Chris@14: if (true !== $constraint->strict) { Chris@14: @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); Chris@0: } Chris@0: Chris@0: if ($constraint->multiple) { Chris@0: foreach ($value as $_value) { Chris@17: if (!\in_array($_value, $choices, $constraint->strict)) { Chris@0: $this->context->buildViolation($constraint->multipleMessage) Chris@0: ->setParameter('{{ value }}', $this->formatValue($_value)) Chris@0: ->setCode(Choice::NO_SUCH_CHOICE_ERROR) Chris@0: ->setInvalidValue($_value) Chris@0: ->addViolation(); Chris@0: Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@17: $count = \count($value); Chris@0: Chris@14: if (null !== $constraint->min && $count < $constraint->min) { Chris@0: $this->context->buildViolation($constraint->minMessage) Chris@0: ->setParameter('{{ limit }}', $constraint->min) Chris@0: ->setPlural((int) $constraint->min) Chris@0: ->setCode(Choice::TOO_FEW_ERROR) Chris@0: ->addViolation(); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@14: if (null !== $constraint->max && $count > $constraint->max) { Chris@0: $this->context->buildViolation($constraint->maxMessage) Chris@0: ->setParameter('{{ limit }}', $constraint->max) Chris@0: ->setPlural((int) $constraint->max) Chris@0: ->setCode(Choice::TOO_MANY_ERROR) Chris@0: ->addViolation(); Chris@0: Chris@0: return; Chris@0: } Chris@17: } elseif (!\in_array($value, $choices, $constraint->strict)) { Chris@0: $this->context->buildViolation($constraint->message) Chris@0: ->setParameter('{{ value }}', $this->formatValue($value)) Chris@0: ->setCode(Choice::NO_SUCH_CHOICE_ERROR) Chris@0: ->addViolation(); Chris@0: } Chris@0: } Chris@0: }