PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Validate/Int.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 148 lines | 76 code | 17 blank | 55 comment | 16 complexity | aba779a53865df1cb31a7cdfa891f21a MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Int.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. */
  86. public function setLocale($locale = null)
  87. {
  88. require_once 'Zend/Locale.php';
  89. $this->_locale = Zend_Locale::findLocale($locale);
  90. return $this;
  91. }
  92. /**
  93. * Defined by Zend_Validate_Interface
  94. *
  95. * Returns true if and only if $value is a valid integer
  96. *
  97. * @param string|integer $value
  98. * @return boolean
  99. */
  100. public function isValid($value)
  101. {
  102. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  103. $this->_error(self::INVALID);
  104. return false;
  105. }
  106. if (is_int($value)) {
  107. return true;
  108. }
  109. $this->_setValue($value);
  110. if ($this->_locale === null) {
  111. $locale = localeconv();
  112. $valueFiltered = str_replace($locale['decimal_point'], '.', $value);
  113. $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered);
  114. if (strval(intval($valueFiltered)) != $valueFiltered) {
  115. $this->_error(self::NOT_INT);
  116. return false;
  117. }
  118. } else {
  119. try {
  120. if (!Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) {
  121. $this->_error(self::NOT_INT);
  122. return false;
  123. }
  124. } catch (Zend_Locale_Exception $e) {
  125. $this->_error(self::NOT_INT);
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. }