|
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
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Check if one date is valid. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Date extends AbstractDate implements RuleInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array Arguments expected. |
|
23
|
|
|
*/ |
|
24
|
|
|
private $arguments = ['string']; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string Valid date. |
|
28
|
|
|
*/ |
|
29
|
|
|
private $date; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var DateTime Valid date in DateTime object. |
|
33
|
|
|
*/ |
|
34
|
|
|
private $dateTimeObject; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string Error message |
|
38
|
|
|
*/ |
|
39
|
|
|
private $message = ''; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Validate. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $received |
|
45
|
|
|
* @param string $format |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
28 |
|
public function validate(string $received, string $format): bool |
|
50
|
|
|
{ |
|
51
|
28 |
|
if ($this->parseDate($received, $format)) { |
|
52
|
19 |
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
9 |
|
$dateTimeObject = DateTime::createFromFormat($format, $received); |
|
56
|
|
|
|
|
57
|
9 |
|
if (!($dateTimeObject instanceof DateTime)) { |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
9 |
|
$this->date = $received; |
|
62
|
9 |
|
$this->dateTimeObject = $dateTimeObject; |
|
63
|
|
|
|
|
64
|
9 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Parse date. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $received |
|
71
|
|
|
* @param string $format |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
28 |
|
private function parseDate(string $received, string $format): bool |
|
76
|
|
|
{ |
|
77
|
28 |
|
$date = date_parse_from_format($format, $received); |
|
78
|
|
|
|
|
79
|
28 |
|
$message = "Received date is not in expected format {$format}"; |
|
80
|
|
|
|
|
81
|
28 |
|
if ($date['warning_count']) { |
|
82
|
9 |
|
$this->message = $message; |
|
83
|
9 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
19 |
|
if ($date['error_count']) { |
|
87
|
10 |
|
$this->message = $message; |
|
88
|
10 |
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Return DateTime object. |
|
96
|
|
|
* |
|
97
|
|
|
* @return DateTime |
|
98
|
|
|
*/ |
|
99
|
6 |
|
public function getDateTimeObject(): DateTime |
|
100
|
|
|
{ |
|
101
|
6 |
|
return $this->dateTimeObject; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return error message. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string Error message |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getMessage(): string |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->message; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|