sfValidatorInteger.class.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorInteger validates an integer. It also converts the input value to an integer.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorInteger.class.php 9048 2008-05-19 09:11:23Z FabianLange $
  16. */
  17. class sfValidatorInteger extends sfValidatorBase
  18. {
  19. /**
  20. * Configures the current validator.
  21. *
  22. * Available options:
  23. *
  24. * * max: The maximum value allowed
  25. * * min: The minimum value allowed
  26. *
  27. * Available error codes:
  28. *
  29. * * max
  30. * * min
  31. *
  32. * @param array $options An array of options
  33. * @param array $messages An array of error messages
  34. *
  35. * @see sfValidatorBase
  36. */
  37. protected function configure($options = array(), $messages = array())
  38. {
  39. $this->addMessage('max', '"%value%" must be less than %max%.');
  40. $this->addMessage('min', '"%value%" must be greater than %min%.');
  41. $this->addOption('min');
  42. $this->addOption('max');
  43. $this->setMessage('invalid', '"%value%" is not an integer.');
  44. }
  45. /**
  46. * @see sfValidatorBase
  47. */
  48. protected function doClean($value)
  49. {
  50. $clean = intval($value);
  51. if (strval($clean) != $value)
  52. {
  53. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  54. }
  55. if ($this->hasOption('max') && $clean > $this->getOption('max'))
  56. {
  57. throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => $this->getOption('max')));
  58. }
  59. if ($this->hasOption('min') && $clean < $this->getOption('min'))
  60. {
  61. throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => $this->getOption('min')));
  62. }
  63. return $clean;
  64. }
  65. }