PageRenderTime 78ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php

https://bitbucket.org/ingsol/music_sonata
PHP | 64 lines | 33 code | 10 blank | 21 comment | 9 complexity | 9983302e3fcfcd2e845d99060a8ed928 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, BSD-3-Clause
  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. * @api
  16. */
  17. class MinLengthValidator extends ConstraintValidator
  18. {
  19. /**
  20. * Checks if the passed value is valid.
  21. *
  22. * @param mixed $value The value that should be validated
  23. * @param Constraint $constraint The constraint for the validation
  24. *
  25. * @return Boolean Whether or not the value is valid
  26. *
  27. * @api
  28. */
  29. public function isValid($value, Constraint $constraint)
  30. {
  31. if (null === $value || '' === $value) {
  32. return true;
  33. }
  34. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  35. throw new UnexpectedTypeException($value, 'string');
  36. }
  37. $value = (string) $value;
  38. if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
  39. $length = grapheme_strlen($value);
  40. } elseif (function_exists('mb_strlen')) {
  41. $length = mb_strlen($value, $constraint->charset);
  42. } else {
  43. $length = strlen($value);
  44. }
  45. if ($length < $constraint->limit) {
  46. $this->setMessage($constraint->message, array(
  47. '{{ value }}' => $value,
  48. '{{ limit }}' => $constraint->limit,
  49. ));
  50. return false;
  51. }
  52. return true;
  53. }
  54. }