MinValidator.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Symfony\Component\Validator\Constraints;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. class MinValidator extends ConstraintValidator
  15. {
  16. public function isValid($value, Constraint $constraint)
  17. {
  18. if ($value === null) {
  19. return true;
  20. }
  21. if (!is_numeric($value)) {
  22. throw new UnexpectedTypeException($value, 'numeric');
  23. }
  24. if ($value < $constraint->limit) {
  25. $this->setMessage($constraint->message, array(
  26. '{{ value }}' => $value,
  27. '{{ limit }}' => $constraint->limit,
  28. ));
  29. return false;
  30. }
  31. return true;
  32. }
  33. }