2013-08-30 17:42:39 +02:00
|
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-08-30 17:42:39 +02:00
|
|
|
|
|
|
|
|
|
namespace Icinga\Web\Form\Validator;
|
|
|
|
|
|
2014-09-03 14:36:18 +02:00
|
|
|
|
use DateTime;
|
2014-08-27 15:51:49 +02:00
|
|
|
|
use Zend_Validate_Abstract;
|
2013-08-30 17:42:39 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2014-09-03 14:36:18 +02:00
|
|
|
|
* Validator for date-and-time input controls
|
|
|
|
|
*
|
|
|
|
|
* @see \Icinga\Web\Form\Element\DateTimePicker For the date-and-time input control.
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*/
|
|
|
|
|
class DateTimeValidator extends Zend_Validate_Abstract
|
|
|
|
|
{
|
2015-02-12 09:04:50 +01:00
|
|
|
|
const INVALID_DATETIME_TYPE = 'invalidDateTimeType';
|
|
|
|
|
const INVALID_DATETIME_FORMAT = 'invalidDateTimeFormat';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The messages to write on differen error states
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*
|
|
|
|
|
* @see Zend_Validate_Abstract::$_messageTemplates‚
|
|
|
|
|
*/
|
|
|
|
|
protected $_messageTemplates = array(
|
|
|
|
|
self::INVALID_DATETIME_TYPE => 'Invalid type given. Instance of DateTime or date/time string expected',
|
|
|
|
|
self::INVALID_DATETIME_FORMAT => 'Date/time string not in the expected format: %value%'
|
|
|
|
|
);
|
|
|
|
|
|
2014-09-03 14:36:18 +02:00
|
|
|
|
protected $local;
|
2013-08-30 17:42:39 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2014-09-03 14:36:18 +02:00
|
|
|
|
* Create a new date-and-time input control validator
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*
|
2014-09-03 14:36:18 +02:00
|
|
|
|
* @param bool $local
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*/
|
2014-09-03 14:36:18 +02:00
|
|
|
|
public function __construct($local)
|
2013-08-30 17:42:39 +02:00
|
|
|
|
{
|
2014-09-03 14:36:18 +02:00
|
|
|
|
$this->local = (bool) $local;
|
2013-08-30 17:42:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-09-03 14:36:18 +02:00
|
|
|
|
* Is the date and time valid?
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*
|
2014-09-03 14:36:18 +02:00
|
|
|
|
* @param string|DateTime $value
|
|
|
|
|
* @param mixed $context
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*
|
2014-09-03 14:36:18 +02:00
|
|
|
|
* @return bool
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*
|
2014-09-03 14:36:18 +02:00
|
|
|
|
* @see \Zend_Validate_Interface::isValid()
|
2013-08-30 17:42:39 +02:00
|
|
|
|
*/
|
|
|
|
|
public function isValid($value, $context = null)
|
|
|
|
|
{
|
2014-09-03 14:36:18 +02:00
|
|
|
|
if (! $value instanceof DateTime && ! is_string($value)) {
|
2015-02-12 09:04:50 +01:00
|
|
|
|
$this->_error(self::INVALID_DATETIME_TYPE);
|
2013-08-30 17:42:39 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-02-17 12:50:11 +01:00
|
|
|
|
|
|
|
|
|
if (! $value instanceof DateTime) {
|
|
|
|
|
$format = $baseFormat = $this->local === true ? 'Y-m-d\TH:i:s' : DateTime::RFC3339;
|
2014-09-03 14:36:18 +02:00
|
|
|
|
$dateTime = DateTime::createFromFormat($format, $value);
|
2015-02-17 12:50:11 +01:00
|
|
|
|
|
|
|
|
|
if ($dateTime === false) {
|
|
|
|
|
$format = substr($format, 0, strrpos($format, ':'));
|
|
|
|
|
$dateTime = DateTime::createFromFormat($format, $value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 14:36:18 +02:00
|
|
|
|
if ($dateTime === false || $dateTime->format($format) !== $value) {
|
2015-05-19 11:22:23 +02:00
|
|
|
|
$this->_error(self::INVALID_DATETIME_FORMAT, $baseFormat);
|
2013-08-30 17:42:39 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-17 12:50:11 +01:00
|
|
|
|
|
2013-08-30 17:42:39 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|