/lib/Zend/Validate/Date.php

https://github.com/sublimino/phpsw · PHP · 251 lines · 128 code · 28 blank · 95 comment · 28 complexity · 77a730744fbfc904ae9218f4eaf20840 MD5 · raw file

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