Date.php 7.5 KB

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