Digits.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Digits extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Validation failure message key for when the value contains non-digit characters
  35. */
  36. const NOT_DIGITS = 'notDigits';
  37. /**
  38. * Validation failure message key for when the value is an empty string
  39. */
  40. const STRING_EMPTY = 'stringEmpty';
  41. /**
  42. * Digits filter used for validation
  43. *
  44. * @var Zend_Filter_Digits
  45. */
  46. protected static $_filter = null;
  47. /**
  48. * Validation failure message template definitions
  49. *
  50. * @var array
  51. */
  52. protected $_messageTemplates = array(
  53. self::NOT_DIGITS => "'%value%' contains not only digit characters",
  54. self::STRING_EMPTY => "'%value%' is an empty string"
  55. );
  56. /**
  57. * Defined by Zend_Validate_Interface
  58. *
  59. * Returns true if and only if $value only contains digit characters
  60. *
  61. * @param string $value
  62. * @return boolean
  63. */
  64. public function isValid($value)
  65. {
  66. $valueString = (string) $value;
  67. $this->_setValue($valueString);
  68. if ('' === $valueString) {
  69. $this->_error(self::STRING_EMPTY);
  70. return false;
  71. }
  72. if (null === self::$_filter) {
  73. /**
  74. * @see Zend_Filter_Digits
  75. */
  76. require_once 'Zend/Filter/Digits.php';
  77. self::$_filter = new Zend_Filter_Digits();
  78. }
  79. if ($valueString !== self::$_filter->filter($valueString)) {
  80. $this->_error(self::NOT_DIGITS);
  81. return false;
  82. }
  83. return true;
  84. }
  85. }