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 AbstractDateTimeComparator extends AbstractParametrizedValidator |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Holds the amount of parameters. |
26
|
|
|
*/ |
27
|
|
|
protected $amountOfParameters = 1; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Holds the type of the validator. |
31
|
|
|
*/ |
32
|
|
|
protected $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Holds whether to parse the parameters as \DateTimes so the child class |
36
|
|
|
* can decide. |
37
|
|
|
*/ |
38
|
|
|
protected $dateTimeParameters = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Compares date times. |
42
|
|
|
* |
43
|
|
|
* @param \DateTime $date the first date to compare |
44
|
|
|
* @param \DateTime[] $datetimes the date times to compare to |
45
|
|
|
* @param array $parameters the original validator parameters |
46
|
|
|
* |
47
|
|
|
* @return boolean - true if the dates compare |
48
|
|
|
*/ |
49
|
|
|
abstract protected function isValidComparison(\DateTime $date, array $datetimes, array $parameters); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets a date time format from the parameters if given or a default one. |
53
|
|
|
* |
54
|
|
|
* @param string[] $parameters the parameters |
55
|
|
|
* |
56
|
|
|
* @return string - the date time format |
57
|
|
|
*/ |
58
|
7 |
|
protected function getDateTimeFormat($parameters) |
59
|
|
|
{ |
60
|
7 |
|
$format = 'Y-m-d H:i:s'; |
61
|
7 |
|
$parametersCount = count($parameters); |
62
|
7 |
|
if ($parametersCount > $this->amountOfParameters) { |
63
|
7 |
|
$format = $parameters[$parametersCount - 1]; |
64
|
|
|
} |
65
|
7 |
|
return $format; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Interprets the given parameters as date times and returns them. |
70
|
|
|
* |
71
|
|
|
* @param array $parameters the parameters |
72
|
|
|
* @param string $format the date time format |
73
|
|
|
* |
74
|
|
|
* @return \DateTime[] - the date times |
75
|
|
|
* |
76
|
|
|
* @throws ValidationException - thrown if one of the parameters is not a date in the given format |
77
|
|
|
*/ |
78
|
7 |
|
protected function getDateTimes(array $parameters, $format) |
79
|
|
|
{ |
80
|
7 |
|
if (!$this->dateTimeParameters) { |
81
|
4 |
|
return []; |
82
|
|
|
} |
83
|
3 |
|
$datetimes = []; |
84
|
3 |
|
for ($i = 0; $i < $this->amountOfParameters; ++$i) { |
85
|
3 |
|
$datetime = \DateTime::createFromFormat($format, $parameters[$i]); |
86
|
3 |
|
if ($datetime === false) { |
87
|
1 |
|
throw new ValidationException('"' . $this->type . '" expects a date of the format "' . $format . '".'); |
88
|
|
|
} |
89
|
3 |
|
$datetimes[] = $datetime; |
90
|
|
|
} |
91
|
3 |
|
return $datetimes; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
7 |
|
public function isValid($value, array $parameters) |
98
|
|
|
{ |
99
|
|
|
|
100
|
7 |
|
if (count($parameters) < $this->amountOfParameters) { |
101
|
4 |
|
throw new ValidationException('"' . $this->type . '" expects at least ' . $this->amountOfParameters . ' parameter.'); |
102
|
|
|
} |
103
|
|
|
|
104
|
7 |
|
if (in_array($value, ['', null], true)) { |
105
|
7 |
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
7 |
|
$format = $this->getDateTimeFormat($parameters); |
109
|
|
|
|
110
|
7 |
|
$datetimes = $this->getDateTimes($parameters, $format); |
111
|
7 |
|
$date = \DateTime::createFromFormat($format, $value); |
112
|
7 |
|
if ($date === false) { |
113
|
7 |
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
7 |
|
return $this->isValidComparison($date, $datetimes, $parameters); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
1 |
|
public function getInvalidDetails() |
123
|
|
|
{ |
124
|
1 |
|
return $this->type; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|