blob: 1e35d610b4f2354df9c0afd5044cec17480eaacc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<?php
namespace ipl\Validator;
use DateTime;
use ipl\I18n\Translation;
/**
* Validator for date-and-time input controls
*/
class DateTimeValidator extends BaseValidator
{
use Translation;
/** @var string Default date time format */
const FORMAT = 'Y-m-d\TH:i:s';
/** @var bool Whether to use the default date time format */
protected $local;
/**
* Create a new date-and-time input control validator
*
* @param bool $local
*/
public function __construct($local = true)
{
$this->local = (bool) $local;
}
/**
* Check whether the given date time is valid
*
* @param string|DateTime $value
*
* @return bool
*/
public function isValid($value)
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();
if (! $value instanceof DateTime && ! is_string($value)) {
$this->addMessage($this->translate('Invalid date/time given.'));
return false;
}
if (! $value instanceof DateTime) {
$format = $this->local === true ? static::FORMAT : DateTime::RFC3339;
$dateTime = DateTime::createFromFormat($format, $value);
if ($dateTime === false || $dateTime->format($format) !== $value) {
$this->addMessage(sprintf(
$this->translate("Date/time string not in the expected format: %s"),
$format
));
return false;
}
}
return true;
}
}
|