PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/Date.php

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