|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Valdi package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Valdi\Validator; |
|
13
|
|
|
|
|
14
|
|
|
use Valdi\ValidationException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Abstract validator to compare date times. |
|
18
|
|
|
* For the format, see: |
|
19
|
|
|
* http://php.net/manual/en/datetime.createfromformat.php |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class DateTimeComparator extends ParametrizedValidator { |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Holds the amount of parameters. |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $amountOfParameters = 1; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Holds the type of the validator. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $type; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Holds whether to parse the parameters as \DateTimes so the child class |
|
35
|
|
|
* can decide. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $parseParametersAsDateTimes = true; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Compares date times. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \DateTime $date |
|
43
|
|
|
* the first date to compare |
|
44
|
|
|
* @param \DateTime[] $datetimes |
|
45
|
|
|
* the date times to compare to |
|
46
|
|
|
* @param array $parameters |
|
47
|
|
|
* the original validator parameters |
|
48
|
|
|
* |
|
49
|
|
|
* @return boolean |
|
50
|
|
|
* true if the dates compare |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function compare(\DateTime $date, array $datetimes, array $parameters); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets a date time format from the parameters if given or a default one. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string[] $parameters |
|
58
|
|
|
* the parameters |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
* the date time format |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getDateTimeFormat($parameters) { |
|
64
|
|
|
$format = 'Y-m-d H:i:s'; |
|
|
|
|
|
|
65
|
|
|
$parametersCount = count($parameters); |
|
66
|
|
|
if ($parametersCount > $this->amountOfParameters) { |
|
67
|
|
|
$format = $parameters[$parametersCount - 1]; |
|
68
|
|
|
} |
|
69
|
|
|
return $format; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Interprets the given parameters as date times and returns them. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $parameters |
|
76
|
|
|
* the paramters |
|
77
|
|
|
* @param string $format |
|
78
|
|
|
* the date time format |
|
79
|
|
|
* |
|
80
|
|
|
* @return \DateTime[] |
|
81
|
|
|
* the date times |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getDateTimes(array $parameters, $format) { |
|
84
|
|
|
$datetimes = array(); |
|
85
|
|
|
for ($i = 0; $i < $this->amountOfParameters; ++$i) { |
|
86
|
|
|
$datetime = \DateTime::createFromFormat($format, $parameters[$i]); |
|
87
|
|
|
if ($datetime === false) { |
|
88
|
|
|
throw new ValidationException('"' . $this->type . '" expects a date of the format "' . $format . '".'); |
|
89
|
|
|
} |
|
90
|
|
|
$datetimes[] = $datetime; |
|
91
|
|
|
} |
|
92
|
|
|
return $datetimes; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function isValid($value, array $parameters) { |
|
99
|
|
|
|
|
100
|
|
|
$this->validateMinParameterCount($this->type, $this->amountOfParameters, $parameters); |
|
101
|
|
|
|
|
102
|
|
|
if (in_array($value, array('', null), true)) { |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$format = $this->getDateTimeFormat($parameters); |
|
107
|
|
|
|
|
108
|
|
|
$datetimes = $this->parseParametersAsDateTimes ? $this->getDateTimes($parameters, $format) : array(); |
|
109
|
|
|
$date = \DateTime::createFromFormat($format, $value); |
|
|
|
|
|
|
110
|
|
|
if ($date === false) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->compare($date, $datetimes, $parameters); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.