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.
16 * @package Zend_Validate
17 * @copyright Copyright (c) 2005-2009 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 17470 2009-08-08 22:27:09Z thomas $
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
28 * @see Zend_Locale_Format
30 require_once 'Zend/Locale/Format.php';
34 * @package Zend_Validate
35 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license http://framework.zend.com/license/new-bsd New BSD License
38 class Zend_Validate_Int
extends Zend_Validate_Abstract
40 const INVALID
= 'intInvalid';
41 const NOT_INT
= 'notInt';
46 protected $_messageTemplates = array(
47 self
::INVALID
=> "Invalid type given, value should be a string or a integer",
48 self
::NOT_INT
=> "'%value%' does not appear to be an integer"
54 * Constructor for the integer validator
56 * @param string|Zend_Locale $locale
58 public function __construct($locale = null)
60 if ($locale !== null) {
61 $this->setLocale($locale);
66 * Returns the set locale
68 public function getLocale()
70 return $this->_locale
;
74 * Sets the locale to use
76 * @param string|Zend_Locale $locale
78 public function setLocale($locale = null)
80 require_once 'Zend/Locale.php';
81 $this->_locale
= Zend_Locale
::findLocale($locale);
86 * Defined by Zend_Validate_Interface
88 * Returns true if and only if $value is a valid integer
90 * @param string|integer $value
93 public function isValid($value)
95 if (!is_string($value) && !is_int($value) && !is_float($value)) {
96 $this->_error(self
::INVALID
);
100 $this->_setValue($value);
101 if ($this->_locale
=== null) {
102 $locale = localeconv();
103 $valueFiltered = str_replace($locale['decimal_point'], '.', $value);
104 $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered);
106 if (strval(intval($valueFiltered)) != $valueFiltered) {
107 $this->_error(self
::NOT_INT
);
113 if (!Zend_Locale_Format
::isInteger($value, array('locale' => 'en')) &&
114 !Zend_Locale_Format
::isInteger($value, array('locale' => $this->_locale
))) {
115 $this->_error(self
::NOT_INT
);
118 } catch (Zend_Locale_Exception
$e) {
119 $this->_error(self
::NOT_INT
);