Length.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Exception\MissingOptionsException;
  13. /**
  14. * @Annotation
  15. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class Length extends Constraint
  20. {
  21. const TOO_SHORT_ERROR = '9ff3fdc4-b214-49db-8718-39c315e33d45';
  22. const TOO_LONG_ERROR = 'd94b19cc-114f-4f44-9cc4-4138e80a87b9';
  23. const INVALID_CHARACTERS_ERROR = '35e6a710-aa2e-4719-b58e-24b35749b767';
  24. protected static $errorNames = array(
  25. self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
  26. self::TOO_LONG_ERROR => 'TOO_LONG_ERROR',
  27. self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
  28. );
  29. public $maxMessage = 'This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less.';
  30. public $minMessage = 'This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.';
  31. public $exactMessage = 'This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.';
  32. public $charsetMessage = 'This value does not match the expected {{ charset }} charset.';
  33. public $max;
  34. public $min;
  35. public $charset = 'UTF-8';
  36. public function __construct($options = null)
  37. {
  38. if (null !== $options && !is_array($options)) {
  39. $options = array(
  40. 'min' => $options,
  41. 'max' => $options,
  42. );
  43. }
  44. parent::__construct($options);
  45. if (null === $this->min && null === $this->max) {
  46. throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), array('min', 'max'));
  47. }
  48. }
  49. }