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 RuleValidateInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array Rule properties |
24
|
|
|
*/ |
25
|
|
|
public static $config = [ |
26
|
|
|
'full_class' => __CLASS__, |
27
|
|
|
'alias' => ['datecompare', 'datcmp', 'dc'], |
28
|
|
|
'args_count' => 3, |
29
|
|
|
'args_type' => ['string', 'string', 'string'] |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string Valid date. |
34
|
|
|
*/ |
35
|
|
|
private $date; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var DateTime Valid date in DateTime object. |
39
|
|
|
*/ |
40
|
|
|
private $dateTimeObject; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string Error message |
44
|
|
|
*/ |
45
|
|
|
private $message = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Validate. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
34 |
|
public function validate(): bool |
53
|
|
|
{ |
54
|
34 |
|
$args = \func_get_args(); |
55
|
|
|
|
56
|
34 |
|
return $this->concreteValidate($args[0], $args[1], $args[2], $args[3]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Concrete validate. |
61
|
|
|
* |
62
|
|
|
* @param string $received |
63
|
|
|
* @param string $operator |
64
|
|
|
* @param string $format |
65
|
|
|
* @param string $compare |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
34 |
|
private function concreteValidate(string $received, string $operator, string $format, string $compare): bool |
70
|
|
|
{ |
71
|
34 |
|
$dateReceived = DateTime::createFromFormat($format, $received); |
72
|
34 |
|
$dateCompare = DateTime::createFromFormat($format, $compare); |
73
|
|
|
|
74
|
34 |
|
if (!($dateReceived && $dateCompare)) { |
75
|
1 |
|
$this->message = "Received date is not in expected format {$format}"; |
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
33 |
|
if ($this->dateHaveNoTime($format)) { |
80
|
18 |
|
$dateReceived->setTime(0, 0, 0); |
81
|
18 |
|
$dateCompare->setTime(0, 0, 0); |
82
|
|
|
} |
83
|
|
|
|
84
|
33 |
|
if ($this->switchOperator($operator, $dateReceived, $dateCompare)) { |
85
|
15 |
|
$this->date = $dateReceived->format($format); |
86
|
15 |
|
$this->dateTimeObject = $dateReceived; |
87
|
15 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
17 |
|
$this->message = "Received date is not {$operator} {$compare}"; |
91
|
|
|
|
92
|
17 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return DateTime object. |
97
|
|
|
* |
98
|
|
|
* @return DateTime |
99
|
|
|
*/ |
100
|
15 |
|
public function getDateTimeObject(): DateTime |
101
|
|
|
{ |
102
|
15 |
|
return $this->dateTimeObject; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Perform correct operation from passed operator. |
107
|
|
|
* |
108
|
|
|
* @param string $operator |
109
|
|
|
* @param DateTime $dateReceived |
110
|
|
|
* @param DateTime $dateCompare |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
* |
114
|
|
|
* @throws UnexpectedValueException if unknown operator is provided. |
115
|
|
|
*/ |
116
|
33 |
|
private function switchOperator(string $operator, DateTime &$dateReceived, DateTime &$dateCompare): bool |
117
|
|
|
{ |
118
|
33 |
|
$received = $dateReceived->format(DateTime::ATOM); |
119
|
33 |
|
$compare = $dateCompare->format(DateTime::ATOM); |
120
|
|
|
|
121
|
33 |
|
switch ($operator) { |
122
|
33 |
|
case '>': //greater than |
123
|
6 |
|
return $received > $compare; |
124
|
27 |
|
case '<': //less than |
125
|
6 |
|
return $received < $compare; |
126
|
21 |
|
case '>=': //greater than or equal |
127
|
6 |
|
return $received >= $compare; |
128
|
15 |
|
case '<=': //less than or equal |
129
|
6 |
|
return $received <= $compare; |
130
|
9 |
|
case '=': //equal |
131
|
8 |
|
return $received === $compare; |
132
|
|
|
default: |
133
|
1 |
|
throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, ="); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return error message. |
139
|
|
|
* |
140
|
|
|
* @return string Error message |
141
|
|
|
*/ |
142
|
1 |
|
public function getMessage(): string |
143
|
|
|
{ |
144
|
1 |
|
return $this->message; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|