Date.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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-2009 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-2009 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. const INVALID = 'dateInvalid';
  34. const NOT_YYYY_MM_DD = 'dateNotYYYY-MM-DD';
  35. const INVALID_DATE = 'dateInvalidDate';
  36. const FALSEFORMAT = 'dateFalseFormat';
  37. /**
  38. * Validation failure message template definitions
  39. *
  40. * @var array
  41. */
  42. protected $_messageTemplates = array(
  43. self::INVALID => "Invalid type given, value should be string, or integer",
  44. self::NOT_YYYY_MM_DD => "'%value%' is not of the format YYYY-MM-DD",
  45. self::INVALID_DATE => "'%value%' does not appear to be a valid date",
  46. self::FALSEFORMAT => "'%value%' does not fit given date format"
  47. );
  48. /**
  49. * Optional format
  50. *
  51. * @var string|null
  52. */
  53. protected $_format;
  54. /**
  55. * Optional locale
  56. *
  57. * @var string|Zend_Locale|null
  58. */
  59. protected $_locale;
  60. /**
  61. * Sets validator options
  62. *
  63. * @param string $format OPTIONAL
  64. * @param string|Zend_Locale $locale OPTIONAL
  65. * @return void
  66. */
  67. public function __construct($format = null, $locale = null)
  68. {
  69. $this->setFormat($format);
  70. if ($locale !== null) {
  71. $this->setLocale($locale);
  72. }
  73. }
  74. /**
  75. * Returns the locale option
  76. *
  77. * @return string|Zend_Locale|null
  78. */
  79. public function getLocale()
  80. {
  81. return $this->_locale;
  82. }
  83. /**
  84. * Sets the locale option
  85. *
  86. * @param string|Zend_Locale $locale
  87. * @return Zend_Validate_Date provides a fluent interface
  88. */
  89. public function setLocale($locale = null)
  90. {
  91. require_once 'Zend/Locale.php';
  92. $this->_locale = Zend_Locale::findLocale($locale);
  93. return $this;
  94. }
  95. /**
  96. * Returns the locale option
  97. *
  98. * @return string|null
  99. */
  100. public function getFormat()
  101. {
  102. return $this->_format;
  103. }
  104. /**
  105. * Sets the format option
  106. *
  107. * @param string $format
  108. * @return Zend_Validate_Date provides a fluent interface
  109. */
  110. public function setFormat($format = null)
  111. {
  112. $this->_format = $format;
  113. return $this;
  114. }
  115. /**
  116. * Defined by Zend_Validate_Interface
  117. *
  118. * Returns true if $value is a valid date of the format YYYY-MM-DD
  119. * If optional $format or $locale is set the date format is checked
  120. * according to Zend_Date, see Zend_Date::isDate()
  121. *
  122. * @param string $value
  123. * @return boolean
  124. */
  125. public function isValid($value)
  126. {
  127. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  128. $this->_error(self::INVALID);
  129. return false;
  130. }
  131. $this->_setValue($value);
  132. if (($this->_format !== null) or ($this->_locale !== null)) {
  133. require_once 'Zend/Date.php';
  134. if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) {
  135. if ($this->_checkFormat($value) === false) {
  136. $this->_error(self::FALSEFORMAT);
  137. } else {
  138. $this->_error(self::INVALID_DATE);
  139. }
  140. return false;
  141. }
  142. } else {
  143. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
  144. $this->_error(self::NOT_YYYY_MM_DD);
  145. return false;
  146. }
  147. list($year, $month, $day) = sscanf($value, '%d-%d-%d');
  148. if (!checkdate($month, $day, $year)) {
  149. $this->_error(self::INVALID_DATE);
  150. return false;
  151. }
  152. }
  153. return true;
  154. }
  155. /**
  156. * Check if the given date fits the given format
  157. *
  158. * @param string $value Date to check
  159. * @return boolean False when date does not fit the format
  160. */
  161. private function _checkFormat($value)
  162. {
  163. try {
  164. require_once 'Zend/Locale/Format.php';
  165. $parsed = Zend_Locale_Format::getDate($value, array(
  166. 'date_format' => $this->_format, 'format_type' => 'iso',
  167. 'fix_date' => false));
  168. if (isset($parsed['year']) and ((strpos(strtoupper($this->_format), 'YY') !== false) and
  169. (strpos(strtoupper($this->_format), 'YYYY') === false))) {
  170. $parsed['year'] = Zend_Date::getFullYear($parsed['year']);
  171. }
  172. } catch (Exception $e) {
  173. // Date can not be parsed
  174. return false;
  175. }
  176. if (((strpos($this->_format, 'Y') !== false) or (strpos($this->_format, 'y') !== false)) and
  177. (!isset($parsed['year']))) {
  178. // Year expected but not found
  179. return false;
  180. }
  181. if ((strpos($this->_format, 'M') !== false) and (!isset($parsed['month']))) {
  182. // Month expected but not found
  183. return false;
  184. }
  185. if ((strpos($this->_format, 'd') !== false) and (!isset($parsed['day']))) {
  186. // Day expected but not found
  187. return false;
  188. }
  189. if (((strpos($this->_format, 'H') !== false) or (strpos($this->_format, 'h') !== false)) and
  190. (!isset($parsed['hour']))) {
  191. // Hour expected but not found
  192. return false;
  193. }
  194. if ((strpos($this->_format, 'm') !== false) and (!isset($parsed['minute']))) {
  195. // Minute expected but not found
  196. return false;
  197. }
  198. if ((strpos($this->_format, 's') !== false) and (!isset($parsed['second']))) {
  199. // Second expected but not found
  200. return false;
  201. }
  202. // Date fits the format
  203. return true;
  204. }
  205. }