|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_Date extends Zend_Validate_Abstract 00034 { 00035 const INVALID = 'dateInvalid'; 00036 const INVALID_DATE = 'dateInvalidDate'; 00037 const FALSEFORMAT = 'dateFalseFormat'; 00038 00044 protected $_messageTemplates = array( 00045 self::INVALID => "Invalid type given, value should be string, integer, array or Zend_Date", 00046 self::INVALID_DATE => "'%value%' does not appear to be a valid date", 00047 self::FALSEFORMAT => "'%value%' does not fit the date format '%format%'", 00048 ); 00049 00053 protected $_messageVariables = array( 00054 'format' => '_format' 00055 ); 00056 00062 protected $_format; 00063 00069 protected $_locale; 00070 00077 public function __construct($options = array()) 00078 { 00079 if ($options instanceof Zend_Config) { 00080 $options = $options->toArray(); 00081 } else if (!is_array($options)) { 00082 $options = func_get_args(); 00083 $temp['format'] = array_shift($options); 00084 if (!empty($options)) { 00085 $temp['locale'] = array_shift($options); 00086 } 00087 00088 $options = $temp; 00089 } 00090 00091 if (array_key_exists('format', $options)) { 00092 $this->setFormat($options['format']); 00093 } 00094 00095 if (!array_key_exists('locale', $options)) { 00096 require_once 'Zend/Registry.php'; 00097 if (Zend_Registry::isRegistered('Zend_Locale')) { 00098 $options['locale'] = Zend_Registry::get('Zend_Locale'); 00099 } 00100 } 00101 00102 if (array_key_exists('locale', $options)) { 00103 $this->setLocale($options['locale']); 00104 } 00105 } 00106 00112 public function getLocale() 00113 { 00114 return $this->_locale; 00115 } 00116 00123 public function setLocale($locale = null) 00124 { 00125 require_once 'Zend/Locale.php'; 00126 $this->_locale = Zend_Locale::findLocale($locale); 00127 return $this; 00128 } 00129 00135 public function getFormat() 00136 { 00137 return $this->_format; 00138 } 00139 00146 public function setFormat($format = null) 00147 { 00148 $this->_format = $format; 00149 return $this; 00150 } 00151 00162 public function isValid($value) 00163 { 00164 if (!is_string($value) && !is_int($value) && !is_float($value) && 00165 !is_array($value) && !($value instanceof Zend_Date)) { 00166 $this->_error(self::INVALID); 00167 return false; 00168 } 00169 00170 $this->_setValue($value); 00171 00172 if (($this->_format !== null) || ($this->_locale !== null) || is_array($value) || 00173 $value instanceof Zend_Date) { 00174 require_once 'Zend/Date.php'; 00175 if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) { 00176 if ($this->_checkFormat($value) === false) { 00177 $this->_error(self::FALSEFORMAT); 00178 } else { 00179 $this->_error(self::INVALID_DATE); 00180 } 00181 return false; 00182 } 00183 } else { 00184 if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) { 00185 $this->_format = 'yyyy-MM-dd'; 00186 $this->_error(self::FALSEFORMAT); 00187 $this->_format = null; 00188 return false; 00189 } 00190 00191 list($year, $month, $day) = sscanf($value, '%d-%d-%d'); 00192 00193 if (!checkdate($month, $day, $year)) { 00194 $this->_error(self::INVALID_DATE); 00195 return false; 00196 } 00197 } 00198 00199 return true; 00200 } 00201 00208 private function _checkFormat($value) 00209 { 00210 try { 00211 require_once 'Zend/Locale/Format.php'; 00212 $parsed = Zend_Locale_Format::getDate($value, array( 00213 'date_format' => $this->_format, 'format_type' => 'iso', 00214 'fix_date' => false)); 00215 if (isset($parsed['year']) and ((strpos(strtoupper($this->_format), 'YY') !== false) and 00216 (strpos(strtoupper($this->_format), 'YYYY') === false))) { 00217 $parsed['year'] = Zend_Date::getFullYear($parsed['year']); 00218 } 00219 } catch (Exception $e) { 00220 // Date can not be parsed 00221 return false; 00222 } 00223 00224 if (((strpos($this->_format, 'Y') !== false) or (strpos($this->_format, 'y') !== false)) and 00225 (!isset($parsed['year']))) { 00226 // Year expected but not found 00227 return false; 00228 } 00229 00230 if ((strpos($this->_format, 'M') !== false) and (!isset($parsed['month']))) { 00231 // Month expected but not found 00232 return false; 00233 } 00234 00235 if ((strpos($this->_format, 'd') !== false) and (!isset($parsed['day']))) { 00236 // Day expected but not found 00237 return false; 00238 } 00239 00240 if (((strpos($this->_format, 'H') !== false) or (strpos($this->_format, 'h') !== false)) and 00241 (!isset($parsed['hour']))) { 00242 // Hour expected but not found 00243 return false; 00244 } 00245 00246 if ((strpos($this->_format, 'm') !== false) and (!isset($parsed['minute']))) { 00247 // Minute expected but not found 00248 return false; 00249 } 00250 00251 if ((strpos($this->_format, 's') !== false) and (!isset($parsed['second']))) { 00252 // Second expected but not found 00253 return false; 00254 } 00255 00256 // Date fits the format 00257 return true; 00258 } 00259 }