Date.php 7.0 KB

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