CommunityID/libs/Zend/Validate/Date.php

239 lines
7.0 KiB
PHP
Raw Normal View History

2019-07-17 20:08:50 +00:00
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
2019-07-17 20:19:00 +00:00
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
2019-07-17 20:08:50 +00:00
* @license http://framework.zend.com/license/new-bsd New BSD License
2019-07-17 20:19:00 +00:00
* @version $Id: Date.php 17696 2009-08-20 20:12:33Z thomas $
2019-07-17 20:08:50 +00:00
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* @category Zend
* @package Zend_Validate
2019-07-17 20:19:00 +00:00
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
2019-07-17 20:08:50 +00:00
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_Date extends Zend_Validate_Abstract
{
const INVALID = 'dateInvalid';
2019-07-17 20:19:00 +00:00
const NOT_YYYY_MM_DD = 'dateNotYYYY-MM-DD';
const INVALID_DATE = 'dateInvalidDate';
2019-07-17 20:08:50 +00:00
const FALSEFORMAT = 'dateFalseFormat';
/**
* Validation failure message template definitions
*
* @var array
*/
protected $_messageTemplates = array(
2019-07-17 20:19:00 +00:00
self::INVALID => "Invalid type given, value should be string, integer, array or Zend_Date",
2019-07-17 20:08:50 +00:00
self::NOT_YYYY_MM_DD => "'%value%' is not of the format YYYY-MM-DD",
2019-07-17 20:19:00 +00:00
self::INVALID_DATE => "'%value%' does not appear to be a valid date",
2019-07-17 20:08:50 +00:00
self::FALSEFORMAT => "'%value%' does not fit given date format"
);
/**
* Optional format
*
* @var string|null
*/
protected $_format;
/**
* Optional locale
*
* @var string|Zend_Locale|null
*/
protected $_locale;
/**
* Sets validator options
*
* @param string $format OPTIONAL
* @param string|Zend_Locale $locale OPTIONAL
* @return void
*/
public function __construct($format = null, $locale = null)
{
$this->setFormat($format);
2019-07-17 20:19:00 +00:00
if ($locale === null) {
require_once 'Zend/Registry.php';
if (Zend_Registry::isRegistered('Zend_Locale')) {
$locale = Zend_Registry::get('Zend_Locale');
}
}
2019-07-17 20:16:19 +00:00
if ($locale !== null) {
$this->setLocale($locale);
}
2019-07-17 20:08:50 +00:00
}
/**
* Returns the locale option
*
* @return string|Zend_Locale|null
*/
public function getLocale()
{
return $this->_locale;
}
/**
* Sets the locale option
*
* @param string|Zend_Locale $locale
* @return Zend_Validate_Date provides a fluent interface
*/
public function setLocale($locale = null)
{
require_once 'Zend/Locale.php';
2019-07-17 20:16:19 +00:00
$this->_locale = Zend_Locale::findLocale($locale);
2019-07-17 20:08:50 +00:00
return $this;
}
/**
* Returns the locale option
*
* @return string|null
*/
public function getFormat()
{
return $this->_format;
}
/**
* Sets the format option
*
* @param string $format
* @return Zend_Validate_Date provides a fluent interface
*/
public function setFormat($format = null)
{
$this->_format = $format;
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if $value is a valid date of the format YYYY-MM-DD
* If optional $format or $locale is set the date format is checked
* according to Zend_Date, see Zend_Date::isDate()
*
2019-07-17 20:19:00 +00:00
* @param string|array|Zend_Date $value
2019-07-17 20:08:50 +00:00
* @return boolean
*/
public function isValid($value)
{
2019-07-17 20:19:00 +00:00
if (!is_string($value) && !is_int($value) && !is_float($value) &&
!is_array($value) && !($value instanceof Zend_Date)) {
$this->_error(self::INVALID);
return false;
}
2019-07-17 20:08:50 +00:00
2019-07-17 20:19:00 +00:00
$this->_setValue($value);
2019-07-17 20:08:50 +00:00
2019-07-17 20:19:00 +00:00
if (($this->_format !== null) || ($this->_locale !== null) || is_array($value) ||
$value instanceof Zend_Date) {
2019-07-17 20:08:50 +00:00
require_once 'Zend/Date.php';
if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) {
if ($this->_checkFormat($value) === false) {
$this->_error(self::FALSEFORMAT);
} else {
2019-07-17 20:19:00 +00:00
$this->_error(self::INVALID_DATE);
2019-07-17 20:08:50 +00:00
}
return false;
}
} else {
2019-07-17 20:19:00 +00:00
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
2019-07-17 20:08:50 +00:00
$this->_error(self::NOT_YYYY_MM_DD);
return false;
}
2019-07-17 20:19:00 +00:00
list($year, $month, $day) = sscanf($value, '%d-%d-%d');
2019-07-17 20:08:50 +00:00
if (!checkdate($month, $day, $year)) {
2019-07-17 20:19:00 +00:00
$this->_error(self::INVALID_DATE);
2019-07-17 20:08:50 +00:00
return false;
}
}
return true;
}
/**
* Check if the given date fits the given format
*
* @param string $value Date to check
* @return boolean False when date does not fit the format
*/
private function _checkFormat($value)
{
try {
require_once 'Zend/Locale/Format.php';
$parsed = Zend_Locale_Format::getDate($value, array(
'date_format' => $this->_format, 'format_type' => 'iso',
'fix_date' => false));
if (isset($parsed['year']) and ((strpos(strtoupper($this->_format), 'YY') !== false) and
(strpos(strtoupper($this->_format), 'YYYY') === false))) {
$parsed['year'] = Zend_Date::getFullYear($parsed['year']);
}
} catch (Exception $e) {
// Date can not be parsed
return false;
}
if (((strpos($this->_format, 'Y') !== false) or (strpos($this->_format, 'y') !== false)) and
(!isset($parsed['year']))) {
// Year expected but not found
return false;
}
if ((strpos($this->_format, 'M') !== false) and (!isset($parsed['month']))) {
// Month expected but not found
return false;
}
if ((strpos($this->_format, 'd') !== false) and (!isset($parsed['day']))) {
// Day expected but not found
return false;
}
if (((strpos($this->_format, 'H') !== false) or (strpos($this->_format, 'h') !== false)) and
(!isset($parsed['hour']))) {
// Hour expected but not found
return false;
}
if ((strpos($this->_format, 'm') !== false) and (!isset($parsed['minute']))) {
// Minute expected but not found
return false;
}
if ((strpos($this->_format, 's') !== false) and (!isset($parsed['second']))) {
// Second expected but not found
return false;
}
// Date fits the format
return true;
}
}