|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Andegna\Validator; |
|
4
|
|
|
|
|
5
|
|
|
class DateValidator implements Validator |
|
6
|
|
|
{ |
|
7
|
|
|
use ValidIntegerValidator; |
|
8
|
|
|
|
|
9
|
|
|
const FIRST_DAY = 1; |
|
10
|
|
|
|
|
11
|
|
|
const FIRST_MONTH = self::FIRST_DAY; |
|
12
|
|
|
|
|
13
|
|
|
const LAST_DAY = 30; |
|
14
|
|
|
|
|
15
|
|
|
const LAST_MONTH = 13; |
|
16
|
|
|
|
|
17
|
|
|
const PAGUME_LAST_DAY = 5; |
|
18
|
|
|
|
|
19
|
|
|
const PAGUME_LEAP_YEAR_LAST_DAY = 6; |
|
20
|
|
|
|
|
21
|
|
|
private $day; |
|
22
|
|
|
|
|
23
|
|
|
private $month; |
|
24
|
|
|
|
|
25
|
|
|
private $year; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct($day, $month, $year) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->day = $day; |
|
30
|
|
|
$this->month = $month; |
|
31
|
|
|
$this->year = $year; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function isValid() |
|
35
|
|
|
{ |
|
36
|
|
|
return |
|
37
|
|
|
$this->isValidInteger($this->day, $this->month, $this->year) && |
|
38
|
|
|
|
|
39
|
|
|
$this->isValidDayRange($this->day) && |
|
40
|
|
|
|
|
41
|
|
|
$this->isValidMonthRange($this->month) && |
|
42
|
|
|
|
|
43
|
|
|
$this->isValidPagumeDayRange($this->day, $this->month) && |
|
44
|
|
|
|
|
45
|
|
|
$this->isValidLeapDay($this->day, $this->month, $this->year); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function isValidDayRange($day) |
|
49
|
|
|
{ |
|
50
|
|
|
return $day >= self::FIRST_DAY && |
|
51
|
|
|
$day <= self::LAST_DAY; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function isValidMonthRange($month) |
|
55
|
|
|
{ |
|
56
|
|
|
return $month >= self::FIRST_MONTH && |
|
57
|
|
|
$month <= self::LAST_MONTH; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function isValidPagumeDayRange($day, $month) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($month === self::LAST_MONTH) { |
|
63
|
|
|
return $day <= self::PAGUME_LEAP_YEAR_LAST_DAY; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function isValidLeapDay($day, $month, $year) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($month === self::LAST_MONTH && |
|
72
|
|
|
$day === self::PAGUME_LEAP_YEAR_LAST_DAY |
|
73
|
|
|
) { |
|
74
|
|
|
return (new LeapYearValidator($year))->isValid(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|