1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Andegna\Validator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Ethiopian DateValidator. |
7
|
|
|
*/ |
8
|
|
|
class DateValidator implements ValidatorInterface |
9
|
|
|
{ |
10
|
|
|
use ValidIntegerValidator; |
11
|
|
|
|
12
|
|
|
const FIRST_DAY = 1; |
13
|
|
|
|
14
|
|
|
const FIRST_MONTH = self::FIRST_DAY; |
15
|
|
|
|
16
|
|
|
const LAST_DAY = 30; |
17
|
|
|
|
18
|
|
|
const LAST_MONTH = 13; |
19
|
|
|
|
20
|
|
|
const PAGUME_LAST_DAY = 5; |
21
|
|
|
|
22
|
|
|
const PAGUME_LEAP_YEAR_LAST_DAY = 6; |
23
|
|
|
|
24
|
|
|
/** @var int */ |
25
|
|
|
protected $day; |
26
|
|
|
|
27
|
|
|
/** @var int */ |
28
|
|
|
protected $month; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
protected $year; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* DateValidator constructor. |
35
|
|
|
* |
36
|
|
|
* @param $day int |
37
|
|
|
* @param $month int |
38
|
|
|
* @param $year int |
39
|
|
|
*/ |
40
|
141 |
|
public function __construct($day, $month, $year) |
41
|
|
|
{ |
42
|
141 |
|
$this->day = $day; |
43
|
141 |
|
$this->month = $month; |
44
|
141 |
|
$this->year = $year; |
45
|
141 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* validate the ethiopian date. |
49
|
|
|
* |
50
|
|
|
* @return bool true if valid |
51
|
|
|
*/ |
52
|
141 |
|
public function isValid() |
53
|
|
|
{ |
54
|
|
|
$validators = [ |
55
|
141 |
|
'isDateValuesIntegers', |
56
|
|
|
'isValidDayRange', |
57
|
|
|
'isValidMonthRange', |
58
|
|
|
'isValidPagumeDayRange', |
59
|
|
|
'isValidLeapDay', |
60
|
|
|
]; |
61
|
|
|
|
62
|
141 |
|
return array_reduce($validators, function ($result, $validator) { |
63
|
141 |
|
return $result && $this->{$validator}(); |
64
|
141 |
|
}, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
135 |
|
protected function isValidDayRange() |
71
|
|
|
{ |
72
|
135 |
|
return $this->day >= self::FIRST_DAY && |
73
|
135 |
|
$this->day <= self::LAST_DAY; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
123 |
|
protected function isValidMonthRange() |
80
|
|
|
{ |
81
|
123 |
|
return $this->month >= self::FIRST_MONTH && |
82
|
123 |
|
$this->month <= self::LAST_MONTH; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
115 |
|
protected function isValidPagumeDayRange() |
89
|
|
|
{ |
90
|
115 |
|
if ($this->month === self::LAST_MONTH) { |
91
|
31 |
|
return $this->day <= self::PAGUME_LEAP_YEAR_LAST_DAY; |
92
|
|
|
} |
93
|
|
|
|
94
|
85 |
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
111 |
|
protected function isValidLeapDay() |
101
|
|
|
{ |
102
|
111 |
|
if ($this->month === self::LAST_MONTH && |
103
|
111 |
|
$this->day === self::PAGUME_LEAP_YEAR_LAST_DAY |
104
|
|
|
) { |
105
|
15 |
|
return (new LeapYearValidator($this->year))->isValid(); |
106
|
|
|
} |
107
|
|
|
|
108
|
97 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
141 |
|
protected function isDateValuesIntegers() |
115
|
|
|
{ |
116
|
141 |
|
return $this->isValidInteger($this->day, $this->month, $this->year); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|