Date.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Date extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Validation failure message key for when the value does not follow the YYYY-MM-DD format
  35. */
  36. const NOT_YYYY_MM_DD = 'dateNotYYYY-MM-DD';
  37. /**
  38. * Validation failure message key for when the value does not appear to be a valid date
  39. */
  40. const INVALID = 'dateInvalid';
  41. /**
  42. * Validation failure message key for when the value does not fit the given dateformat or locale
  43. */
  44. const FALSEFORMAT = 'dateFalseFormat';
  45. /**
  46. * Validation failure message template definitions
  47. *
  48. * @var array
  49. */
  50. protected $_messageTemplates = array(
  51. self::NOT_YYYY_MM_DD => "'%value%' is not of the format YYYY-MM-DD",
  52. self::INVALID => "'%value%' does not appear to be a valid date",
  53. self::FALSEFORMAT => "'%value%' does not fit given date format"
  54. );
  55. /**
  56. * Optional format
  57. *
  58. * @var string|null
  59. */
  60. protected $_format;
  61. /**
  62. * Optional locale
  63. *
  64. * @var string|Zend_Locale|null
  65. */
  66. protected $_locale;
  67. /**
  68. * Sets validator options
  69. *
  70. * @param string $format OPTIONAL
  71. * @param string|Zend_Locale $locale OPTIONAL
  72. * @return void
  73. */
  74. public function __construct($format = null, $locale = null)
  75. {
  76. $this->setFormat($format);
  77. if ($locale !== null) {
  78. $this->setLocale($locale);
  79. }
  80. }
  81. /**
  82. * Returns the locale option
  83. *
  84. * @return string|Zend_Locale|null
  85. */
  86. public function getLocale()
  87. {
  88. return $this->_locale;
  89. }
  90. /**
  91. * Sets the locale option
  92. *
  93. * @param string|Zend_Locale $locale
  94. * @return Zend_Validate_Date provides a fluent interface
  95. */
  96. public function setLocale($locale = null)
  97. {
  98. require_once 'Zend/Locale.php';
  99. $this->_locale = Zend_Locale::findLocale($locale);
  100. return $this;
  101. }
  102. /**
  103. * Returns the locale option
  104. *
  105. * @return string|null
  106. */
  107. public function getFormat()
  108. {
  109. return $this->_format;
  110. }
  111. /**
  112. * Sets the format option
  113. *
  114. * @param string $format
  115. * @return Zend_Validate_Date provides a fluent interface
  116. */
  117. public function setFormat($format = null)
  118. {
  119. $this->_format = $format;
  120. return $this;
  121. }
  122. /**
  123. * Defined by Zend_Validate_Interface
  124. *
  125. * Returns true if $value is a valid date of the format YYYY-MM-DD
  126. * If optional $format or $locale is set the date format is checked
  127. * according to Zend_Date, see Zend_Date::isDate()
  128. *
  129. * @param string $value
  130. * @return boolean
  131. */
  132. public function isValid($value)
  133. {
  134. $valueString = (string) $value;
  135. $this->_setValue($valueString);
  136. if (($this->_format !== null) or ($this->_locale !== null)) {
  137. require_once 'Zend/Date.php';
  138. if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) {
  139. if ($this->_checkFormat($value) === false) {
  140. $this->_error(self::FALSEFORMAT);
  141. } else {
  142. $this->_error(self::INVALID);
  143. }
  144. return false;
  145. }
  146. } else {
  147. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $valueString)) {
  148. $this->_error(self::NOT_YYYY_MM_DD);
  149. return false;
  150. }
  151. list($year, $month, $day) = sscanf($valueString, '%d-%d-%d');
  152. if (!checkdate($month, $day, $year)) {
  153. $this->_error(self::INVALID);
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. /**
  160. * Check if the given date fits the given format
  161. *
  162. * @param string $value Date to check
  163. * @return boolean False when date does not fit the format
  164. */
  165. private function _checkFormat($value)
  166. {
  167. try {
  168. require_once 'Zend/Locale/Format.php';
  169. $parsed = Zend_Locale_Format::getDate($value, array(
  170. 'date_format' => $this->_format, 'format_type' => 'iso',
  171. 'fix_date' => false));
  172. if (isset($parsed['year']) and ((strpos(strtoupper($this->_format), 'YY') !== false) and
  173. (strpos(strtoupper($this->_format), 'YYYY') === false))) {
  174. $parsed['year'] = Zend_Date::getFullYear($parsed['year']);
  175. }
  176. } catch (Exception $e) {
  177. // Date can not be parsed
  178. return false;
  179. }
  180. if (((strpos($this->_format, 'Y') !== false) or (strpos($this->_format, 'y') !== false)) and
  181. (!isset($parsed['year']))) {
  182. // Year expected but not found
  183. return false;
  184. }
  185. if ((strpos($this->_format, 'M') !== false) and (!isset($parsed['month']))) {
  186. // Month expected but not found
  187. return false;
  188. }
  189. if ((strpos($this->_format, 'd') !== false) and (!isset($parsed['day']))) {
  190. // Day expected but not found
  191. return false;
  192. }
  193. if (((strpos($this->_format, 'H') !== false) or (strpos($this->_format, 'h') !== false)) and
  194. (!isset($parsed['hour']))) {
  195. // Hour expected but not found
  196. return false;
  197. }
  198. if ((strpos($this->_format, 'm') !== false) and (!isset($parsed['minute']))) {
  199. // Minute expected but not found
  200. return false;
  201. }
  202. if ((strpos($this->_format, 's') !== false) and (!isset($parsed['second']))) {
  203. // Second expected but not found
  204. return false;
  205. }
  206. // Date fits the format
  207. return true;
  208. }
  209. }