From c53d8d026021f97075fb2f4940ba22793c38fb6e Mon Sep 17 00:00:00 2001 From: davehauenstein Date: Wed, 15 Apr 2009 22:06:42 +0000 Subject: added toolbar; functionality includes refresh button to get back to original page, print article, email a link to the article with a personal note git-svn-id: http://arc90labs-readability.googlecode.com/svn/trunk@31 d4e419ec-0920-11de-bbfd-a7c1bc4c261e --- lib/Zend/Validate/Date.php | 235 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 lib/Zend/Validate/Date.php (limited to 'lib/Zend/Validate/Date.php') diff --git a/lib/Zend/Validate/Date.php b/lib/Zend/Validate/Date.php new file mode 100644 index 0000000..156c584 --- /dev/null +++ b/lib/Zend/Validate/Date.php @@ -0,0 +1,235 @@ + "'%value%' is not of the format YYYY-MM-DD", + self::INVALID => "'%value%' does not appear to be a valid date", + self::FALSEFORMAT => "'%value%' does not fit given date format" + ); + + /** + * Optional format + * + * @var string|null + */ + protected $_format; + + /** + * Optional locale + * + * @var string|Zend_Locale|null + */ + protected $_locale; + + /** + * Sets validator options + * + * @param string $format OPTIONAL + * @param string|Zend_Locale $locale OPTIONAL + * @return void + */ + public function __construct($format = null, $locale = null) + { + $this->setFormat($format); + if ($locale !== null) { + $this->setLocale($locale); + } + } + + /** + * Returns the locale option + * + * @return string|Zend_Locale|null + */ + public function getLocale() + { + return $this->_locale; + } + + /** + * Sets the locale option + * + * @param string|Zend_Locale $locale + * @return Zend_Validate_Date provides a fluent interface + */ + public function setLocale($locale = null) + { + require_once 'Zend/Locale.php'; + $this->_locale = Zend_Locale::findLocale($locale); + return $this; + } + + /** + * Returns the locale option + * + * @return string|null + */ + public function getFormat() + { + return $this->_format; + } + + /** + * Sets the format option + * + * @param string $format + * @return Zend_Validate_Date provides a fluent interface + */ + public function setFormat($format = null) + { + $this->_format = $format; + return $this; + } + + /** + * Defined by Zend_Validate_Interface + * + * Returns true if $value is a valid date of the format YYYY-MM-DD + * If optional $format or $locale is set the date format is checked + * according to Zend_Date, see Zend_Date::isDate() + * + * @param string $value + * @return boolean + */ + public function isValid($value) + { + $valueString = (string) $value; + + $this->_setValue($valueString); + + if (($this->_format !== null) or ($this->_locale !== null)) { + require_once 'Zend/Date.php'; + if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) { + if ($this->_checkFormat($value) === false) { + $this->_error(self::FALSEFORMAT); + } else { + $this->_error(self::INVALID); + } + return false; + } + } else { + if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $valueString)) { + $this->_error(self::NOT_YYYY_MM_DD); + return false; + } + + list($year, $month, $day) = sscanf($valueString, '%d-%d-%d'); + + if (!checkdate($month, $day, $year)) { + $this->_error(self::INVALID); + return false; + } + } + + return true; + } + + /** + * Check if the given date fits the given format + * + * @param string $value Date to check + * @return boolean False when date does not fit the format + */ + private function _checkFormat($value) + { + try { + require_once 'Zend/Locale/Format.php'; + $parsed = Zend_Locale_Format::getDate($value, array( + 'date_format' => $this->_format, 'format_type' => 'iso', + 'fix_date' => false)); + if (isset($parsed['year']) and ((strpos(strtoupper($this->_format), 'YY') !== false) and + (strpos(strtoupper($this->_format), 'YYYY') === false))) { + $parsed['year'] = Zend_Date::getFullYear($parsed['year']); + } + } catch (Exception $e) { + // Date can not be parsed + return false; + } + + if (((strpos($this->_format, 'Y') !== false) or (strpos($this->_format, 'y') !== false)) and + (!isset($parsed['year']))) { + // Year expected but not found + return false; + } + + if ((strpos($this->_format, 'M') !== false) and (!isset($parsed['month']))) { + // Month expected but not found + return false; + } + + if ((strpos($this->_format, 'd') !== false) and (!isset($parsed['day']))) { + // Day expected but not found + return false; + } + + if (((strpos($this->_format, 'H') !== false) or (strpos($this->_format, 'h') !== false)) and + (!isset($parsed['hour']))) { + // Hour expected but not found + return false; + } + + if ((strpos($this->_format, 'm') !== false) and (!isset($parsed['minute']))) { + // Minute expected but not found + return false; + } + + if ((strpos($this->_format, 's') !== false) and (!isset($parsed['second']))) { + // Second expected but not found + return false; + } + + // Date fits the format + return true; + } +} -- cgit v1.2.3