|
Moodle
2.2.1
http://www.collinsharper.com
|
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 string or 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 instanceof Zend_Config) { 00061 $locale = $locale->toArray(); 00062 } 00063 00064 if (is_array($locale)) { 00065 if (array_key_exists('locale', $locale)) { 00066 $locale = $locale['locale']; 00067 } else { 00068 $locale = null; 00069 } 00070 } 00071 00072 if (empty($locale)) { 00073 require_once 'Zend/Registry.php'; 00074 if (Zend_Registry::isRegistered('Zend_Locale')) { 00075 $locale = Zend_Registry::get('Zend_Locale'); 00076 } 00077 } 00078 00079 if ($locale !== null) { 00080 $this->setLocale($locale); 00081 } 00082 } 00083 00087 public function getLocale() 00088 { 00089 return $this->_locale; 00090 } 00091 00097 public function setLocale($locale = null) 00098 { 00099 require_once 'Zend/Locale.php'; 00100 $this->_locale = Zend_Locale::findLocale($locale); 00101 return $this; 00102 } 00103 00112 public function isValid($value) 00113 { 00114 if (!is_string($value) && !is_int($value) && !is_float($value)) { 00115 $this->_error(self::INVALID); 00116 return false; 00117 } 00118 00119 if (is_int($value)) { 00120 return true; 00121 } 00122 00123 $this->_setValue($value); 00124 if ($this->_locale === null) { 00125 $locale = localeconv(); 00126 $valueFiltered = str_replace($locale['decimal_point'], '.', $value); 00127 $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered); 00128 00129 if (strval(intval($valueFiltered)) != $valueFiltered) { 00130 $this->_error(self::NOT_INT); 00131 return false; 00132 } 00133 00134 } else { 00135 try { 00136 if (!Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) { 00137 $this->_error(self::NOT_INT); 00138 return false; 00139 } 00140 } catch (Zend_Locale_Exception $e) { 00141 $this->_error(self::NOT_INT); 00142 return false; 00143 } 00144 } 00145 00146 return true; 00147 } 00148 }