-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
DateTimeValidator.php
92 lines (82 loc) · 3.05 KB
/
DateTimeValidator.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Neos\Flow\Validation\Validator;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\Cldr\Reader\DatesReader;
use Neos\Flow\I18n;
/**
* Validator for DateTime objects.
*
* @api
*/
class DateTimeValidator extends AbstractValidator
{
/**
* @var array
*/
protected $supportedOptions = [
'locale' => [null, 'The locale to use for date parsing', 'string|Locale'],
'strictMode' => [true, 'Use strict mode for date parsing', 'boolean'],
'formatLength' => [DatesReader::FORMAT_LENGTH_DEFAULT, 'The format length, see DatesReader::FORMAT_LENGTH_*', 'string'],
'formatType' => [DatesReader::FORMAT_TYPE_DATE, 'The format type, see DatesReader::FORMAT_TYPE_*', 'string']
];
/**
* @Flow\Inject
* @var I18n\Service
*/
protected $localizationService;
/**
* @Flow\Inject
* @var I18n\Parser\DatetimeParser
*/
protected $datetimeParser;
/**
* Checks if the given value is a valid DateTime object.
*
* @param mixed $value The value that should be validated
* @return void
* @api
*/
protected function isValid($value)
{
if ($value instanceof \DateTimeInterface) {
return;
}
if (!isset($this->options['locale'])) {
$locale = $this->localizationService->getConfiguration()->getDefaultLocale();
} elseif (is_string($this->options['locale'])) {
$locale = new I18n\Locale($this->options['locale']);
} elseif ($this->options['locale'] instanceof I18n\Locale) {
$locale = $this->options['locale'];
} else {
$this->addError('The "locale" option can be only set to string identifier, or Locale object.', 1281454676);
return;
}
$strictMode = $this->options['strictMode'];
$formatLength = $this->options['formatLength'];
DatesReader::validateFormatLength($formatLength);
$formatType = $this->options['formatType'];
DatesReader::validateFormatType($formatType);
if ($formatType === DatesReader::FORMAT_TYPE_TIME) {
if ($this->datetimeParser->parseTime($value, $locale, $formatLength, $strictMode) === false) {
$this->addError('A valid time is expected.', 1281454830);
}
} elseif ($formatType === DatesReader::FORMAT_TYPE_DATETIME) {
if ($this->datetimeParser->parseDateAndTime($value, $locale, $formatLength, $strictMode) === false) {
$this->addError('A valid date and time is expected.', 1281454831);
}
} else {
if ($this->datetimeParser->parseDate($value, $locale, $formatLength, $strictMode) === false) {
$this->addError('A valid date is expected.', 1281454832);
}
}
}
}