00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00030 require_once 'Zend/Locale/Format.php'; 00031 00038 class Zend_Validate_Int extends Zend_Validate_Abstract 00039 { 00040 const INVALID = 'intInvalid'; 00041 const NOT_INT = 'notInt'; 00042 00046 protected $_messageTemplates = array( 00047 self::INVALID => "Invalid type given, value should be a string or a integer", 00048 self::NOT_INT => "'%value%' does not appear to be an integer" 00049 ); 00050 00051 protected $_locale; 00052 00058 public function __construct($locale = null) 00059 { 00060 if ($locale !== null) { 00061 $this->setLocale($locale); 00062 } 00063 } 00064 00068 public function getLocale() 00069 { 00070 return $this->_locale; 00071 } 00072 00078 public function setLocale($locale = null) 00079 { 00080 require_once 'Zend/Locale.php'; 00081 $this->_locale = Zend_Locale::findLocale($locale); 00082 return $this; 00083 } 00084 00093 public function isValid($value) 00094 { 00095 if (!is_string($value) && !is_int($value) && !is_float($value)) { 00096 $this->_error(self::INVALID); 00097 return false; 00098 } 00099 00100 $this->_setValue($value); 00101 if ($this->_locale === null) { 00102 $locale = localeconv(); 00103 $valueFiltered = str_replace($locale['decimal_point'], '.', $value); 00104 $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered); 00105 00106 if (strval(intval($valueFiltered)) != $valueFiltered) { 00107 $this->_error(self::NOT_INT); 00108 return false; 00109 } 00110 00111 } else { 00112 try { 00113 if (!Zend_Locale_Format::isInteger($value, array('locale' => 'en')) && 00114 !Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) { 00115 $this->_error(self::NOT_INT); 00116 return false; 00117 } 00118 } catch (Zend_Locale_Exception $e) { 00119 $this->_error(self::NOT_INT); 00120 return false; 00121 } 00122 } 00123 00124 return true; 00125 } 00126 }