IsFalseValidator.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class IsFalseValidator extends ConstraintValidator
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function validate($value, Constraint $constraint)
  23. {
  24. if (!$constraint instanceof IsFalse) {
  25. throw new UnexpectedTypeException($constraint, IsFalse::class);
  26. }
  27. if (null === $value || false === $value || 0 === $value || '0' === $value) {
  28. return;
  29. }
  30. $this->context->buildViolation($constraint->message)
  31. ->setParameter('{{ value }}', $this->formatValue($value))
  32. ->setCode(IsFalse::NOT_FALSE_ERROR)
  33. ->addViolation();
  34. }
  35. }