Test Failed
Branch develop (09cda0)
by Samson
03:06
created

DateValidator::isValid()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 5
nop 0
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