|
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_Float extends Zend_Validate_Abstract 00039 { 00040 const INVALID = 'floatInvalid'; 00041 const NOT_FLOAT = 'notFloat'; 00042 00046 protected $_messageTemplates = array( 00047 self::INVALID => "Invalid type given, value should be float, string, or integer", 00048 self::NOT_FLOAT => "'%value%' does not appear to be a float", 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 $this->setLocale($locale); 00080 } 00081 00085 public function getLocale() 00086 { 00087 return $this->_locale; 00088 } 00089 00095 public function setLocale($locale = null) 00096 { 00097 require_once 'Zend/Locale.php'; 00098 $this->_locale = Zend_Locale::findLocale($locale); 00099 return $this; 00100 } 00101 00110 public function isValid($value) 00111 { 00112 if (!is_string($value) && !is_int($value) && !is_float($value)) { 00113 $this->_error(self::INVALID); 00114 return false; 00115 } 00116 00117 if (is_float($value)) { 00118 return true; 00119 } 00120 00121 $this->_setValue($value); 00122 try { 00123 if (!Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) { 00124 $this->_error(self::NOT_FLOAT); 00125 return false; 00126 } 00127 } catch (Zend_Locale_Exception $e) { 00128 $this->_error(self::NOT_FLOAT); 00129 return false; 00130 } 00131 00132 return true; 00133 } 00134 }