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

/src/application/libraries/Zend/Validate/Date.php

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