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 NOT_YYYY_MM_DD = 'dateNotYYYY-MM-DD';
00037 const INVALID_DATE = 'dateInvalidDate';
00038 const FALSEFORMAT = 'dateFalseFormat';
00039
00045 protected $_messageTemplates = array(
00046 self::INVALID => "Invalid type given, value should be string, integer, array or Zend_Date",
00047 self::NOT_YYYY_MM_DD => "'%value%' is not of the format YYYY-MM-DD",
00048 self::INVALID_DATE => "'%value%' does not appear to be a valid date",
00049 self::FALSEFORMAT => "'%value%' does not fit given date format"
00050 );
00051
00057 protected $_format;
00058
00064 protected $_locale;
00065
00073 public function __construct($format = null, $locale = null)
00074 {
00075 $this->setFormat($format);
00076 if ($locale === null) {
00077 require_once 'Zend/Registry.php';
00078 if (Zend_Registry::isRegistered('Zend_Locale')) {
00079 $locale = Zend_Registry::get('Zend_Locale');
00080 }
00081 }
00082
00083 if ($locale !== null) {
00084 $this->setLocale($locale);
00085 }
00086 }
00087
00093 public function getLocale()
00094 {
00095 return $this->_locale;
00096 }
00097
00104 public function setLocale($locale = null)
00105 {
00106 require_once 'Zend/Locale.php';
00107 $this->_locale = Zend_Locale::findLocale($locale);
00108 return $this;
00109 }
00110
00116 public function getFormat()
00117 {
00118 return $this->_format;
00119 }
00120
00127 public function setFormat($format = null)
00128 {
00129 $this->_format = $format;
00130 return $this;
00131 }
00132
00143 public function isValid($value)
00144 {
00145 if (!is_string($value) && !is_int($value) && !is_float($value) &&
00146 !is_array($value) && !($value instanceof Zend_Date)) {
00147 $this->_error(self::INVALID);
00148 return false;
00149 }
00150
00151 $this->_setValue($value);
00152
00153 if (($this->_format !== null) || ($this->_locale !== null) || is_array($value) ||
00154 $value instanceof Zend_Date) {
00155 require_once 'Zend/Date.php';
00156 if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) {
00157 if ($this->_checkFormat($value) === false) {
00158 $this->_error(self::FALSEFORMAT);
00159 } else {
00160 $this->_error(self::INVALID_DATE);
00161 }
00162 return false;
00163 }
00164 } else {
00165 if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
00166 $this->_error(self::NOT_YYYY_MM_DD);
00167 return false;
00168 }
00169
00170 list($year, $month, $day) = sscanf($value, '%d-%d-%d');
00171
00172 if (!checkdate($month, $day, $year)) {
00173 $this->_error(self::INVALID_DATE);
00174 return false;
00175 }
00176 }
00177
00178 return true;
00179 }
00180
00187 private function _checkFormat($value)
00188 {
00189 try {
00190 require_once 'Zend/Locale/Format.php';
00191 $parsed = Zend_Locale_Format::getDate($value, array(
00192 'date_format' => $this->_format, 'format_type' => 'iso',
00193 'fix_date' => false));
00194 if (isset($parsed['year']) and ((strpos(strtoupper($this->_format), 'YY') !== false) and
00195 (strpos(strtoupper($this->_format), 'YYYY') === false))) {
00196 $parsed['year'] = Zend_Date::getFullYear($parsed['year']);
00197 }
00198 } catch (Exception $e) {
00199
00200 return false;
00201 }
00202
00203 if (((strpos($this->_format, 'Y') !== false) or (strpos($this->_format, 'y') !== false)) and
00204 (!isset($parsed['year']))) {
00205
00206 return false;
00207 }
00208
00209 if ((strpos($this->_format, 'M') !== false) and (!isset($parsed['month']))) {
00210
00211 return false;
00212 }
00213
00214 if ((strpos($this->_format, 'd') !== false) and (!isset($parsed['day']))) {
00215
00216 return false;
00217 }
00218
00219 if (((strpos($this->_format, 'H') !== false) or (strpos($this->_format, 'h') !== false)) and
00220 (!isset($parsed['hour']))) {
00221
00222 return false;
00223 }
00224
00225 if ((strpos($this->_format, 'm') !== false) and (!isset($parsed['minute']))) {
00226
00227 return false;
00228 }
00229
00230 if ((strpos($this->_format, 's') !== false) and (!isset($parsed['second']))) {
00231
00232 return false;
00233 }
00234
00235
00236 return true;
00237 }
00238 }