|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Filter |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types = 1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Filter\Rules; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use UnexpectedValueException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Compare two dates using >, <, >=, <=, = operators. |
|
19
|
|
|
*/ |
|
20
|
|
|
class DateCompare extends AbstractDate implements RuleInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array Arguments expected. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $arguments = ['string', 'string', 'string']; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string Valid date. |
|
29
|
|
|
*/ |
|
30
|
|
|
private $date; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var DateTime Valid date in DateTime object. |
|
34
|
|
|
*/ |
|
35
|
|
|
private $dateTimeObject; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string Error message |
|
39
|
|
|
*/ |
|
40
|
|
|
private $message = ''; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Validate. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $received |
|
46
|
|
|
* @param string $operator |
|
47
|
|
|
* @param string $format |
|
48
|
|
|
* @param string $compare |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
33 |
|
public function validate(string $received, string $operator, string $format, string $compare): bool |
|
53
|
|
|
{ |
|
54
|
33 |
|
$dateReceived = DateTime::createFromFormat($format, $received); |
|
55
|
33 |
|
$dateCompare = DateTime::createFromFormat($format, $compare); |
|
56
|
|
|
|
|
57
|
33 |
|
if (!($dateReceived && $dateCompare)) { |
|
58
|
1 |
|
$this->message = "Received date is not in expected format {$format}"; |
|
59
|
1 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
32 |
|
if ($this->dateHaveNoTime($format)) { |
|
63
|
17 |
|
$dateReceived->setTime(0, 0, 0); |
|
64
|
17 |
|
$dateCompare->setTime(0, 0, 0); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
32 |
|
if ($this->switchOperator($operator, $dateReceived, $dateCompare)) { |
|
68
|
15 |
|
$this->date = $dateReceived->format($format); |
|
69
|
15 |
|
$this->dateTimeObject = $dateReceived; |
|
70
|
15 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
16 |
|
$this->message = "Received date is not {$operator} {$compare}"; |
|
74
|
|
|
|
|
75
|
16 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Return DateTime object. |
|
80
|
|
|
* |
|
81
|
|
|
* @return DateTime |
|
82
|
|
|
*/ |
|
83
|
15 |
|
public function getDateTimeObject(): DateTime |
|
84
|
|
|
{ |
|
85
|
15 |
|
return $this->dateTimeObject; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Perform correct operation from passed operator. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $operator |
|
92
|
|
|
* @param DateTime $dateReceived |
|
93
|
|
|
* @param DateTime $dateCompare |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
* |
|
97
|
|
|
* @throws UnexpectedValueException if unknown operator is provided. |
|
98
|
|
|
*/ |
|
99
|
32 |
|
private function switchOperator(string $operator, DateTime &$dateReceived, DateTime &$dateCompare): bool |
|
100
|
|
|
{ |
|
101
|
32 |
|
$received = $dateReceived->format(DateTime::ATOM); |
|
102
|
32 |
|
$compare = $dateCompare->format(DateTime::ATOM); |
|
103
|
|
|
|
|
104
|
|
|
switch ($operator) { |
|
105
|
32 |
|
case '>': //greater than |
|
106
|
6 |
|
return $received > $compare; |
|
107
|
26 |
|
case '<': //less than |
|
108
|
6 |
|
return $received < $compare; |
|
109
|
20 |
|
case '>=': //greater than or equal |
|
110
|
6 |
|
return $received >= $compare; |
|
111
|
14 |
|
case '<=': //less than or equal |
|
112
|
6 |
|
return $received <= $compare; |
|
113
|
8 |
|
case '=': //equal |
|
114
|
7 |
|
return $received === $compare; |
|
115
|
|
|
default: |
|
116
|
1 |
|
throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, ="); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Return error message. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string Error message |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getMessage(): string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->message; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|