Int.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-2015 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. * @see Zend_Locale_Format
  27. */
  28. #require_once 'Zend/Locale/Format.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Validate_Int extends Zend_Validate_Abstract
  36. {
  37. const INVALID = 'intInvalid';
  38. const NOT_INT = 'notInt';
  39. /**
  40. * @var array
  41. */
  42. protected $_messageTemplates = array(
  43. self::INVALID => "Invalid type given. String or integer expected",
  44. self::NOT_INT => "'%value%' does not appear to be an integer",
  45. );
  46. protected $_locale;
  47. /**
  48. * Constructor for the integer validator
  49. *
  50. * @param string|Zend_Config|Zend_Locale $locale
  51. */
  52. public function __construct($locale = null)
  53. {
  54. if ($locale instanceof Zend_Config) {
  55. $locale = $locale->toArray();
  56. }
  57. if (is_array($locale)) {
  58. if (array_key_exists('locale', $locale)) {
  59. $locale = $locale['locale'];
  60. } else {
  61. $locale = null;
  62. }
  63. }
  64. if (empty($locale)) {
  65. #require_once 'Zend/Registry.php';
  66. if (Zend_Registry::isRegistered('Zend_Locale')) {
  67. $locale = Zend_Registry::get('Zend_Locale');
  68. }
  69. }
  70. if ($locale !== null) {
  71. $this->setLocale($locale);
  72. }
  73. }
  74. /**
  75. * Returns the set locale
  76. */
  77. public function getLocale()
  78. {
  79. return $this->_locale;
  80. }
  81. /**
  82. * Sets the locale to use
  83. *
  84. * @param string|Zend_Locale $locale
  85. * @return $this
  86. */
  87. public function setLocale($locale = null)
  88. {
  89. #require_once 'Zend/Locale.php';
  90. $this->_locale = Zend_Locale::findLocale($locale);
  91. return $this;
  92. }
  93. /**
  94. * Defined by Zend_Validate_Interface
  95. *
  96. * Returns true if and only if $value is a valid integer
  97. *
  98. * @param string|integer $value
  99. * @return boolean
  100. */
  101. public function isValid($value)
  102. {
  103. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  104. $this->_error(self::INVALID);
  105. return false;
  106. }
  107. if (is_int($value)) {
  108. return true;
  109. }
  110. $this->_setValue($value);
  111. if ($this->_locale === null) {
  112. $locale = localeconv();
  113. $valueFiltered = str_replace($locale['decimal_point'], '.', $value);
  114. $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered);
  115. if (strval(intval($valueFiltered)) != $valueFiltered) {
  116. $this->_error(self::NOT_INT);
  117. return false;
  118. }
  119. } else {
  120. try {
  121. if (!Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) {
  122. $this->_error(self::NOT_INT);
  123. return false;
  124. }
  125. } catch (Zend_Locale_Exception $e) {
  126. $this->_error(self::NOT_INT);
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. }