Calender::validateInput()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
namespace AlgoWeb\xsdTypes\AxillaryClasses;
3
4
class Calender
5
{
6
    private $year;
7
    private $month;
8
    private $day;
9
    private $timeZone;
10
11
    /**
12
     * Calender constructor.
13
     *
14
     * @param string|null $year
15
     * @param string|null $month
16
     * @param string|null $day
17
     * @param string|null $timezone
18
     */
19
    protected function __construct($year = null, $month = null, $day = null, $timezone = null)
20
    {
21
        $this->validateInput($year, $month, $day);
22
        $this->year = $year;
23
        $this->month = $month;
24
        $this->day = $day;
25
        $this->timeZone = $timezone;
26
        $this->validateState();
27
    }
28
29
    /**
30
     * @param string|null $year
31
     * @param string|null $month
32
     * @param string|null $day
33
     *
34
     * @return void
35
     */
36
    private function validateInput($year, $month, $day)
37
    {
38
        $this->validateInputNotAllNull($year, $month, $day);
39
        $this->validateInputNotYearAndDay($year, $month, $day);
40
    }
41
42
    private function validateInputNotAllNull($year, $month, $day)
43
    {
44 View Code Duplication
        if (null === $year && null === $month && null === $day) {
45
            throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year');
46
        }
47
    }
48
49
    private function validateInputNotYearAndDay($year, $month, $day)
50
    {
51 View Code Duplication
        if (null !== $year && null === $month && null !== $day) {
52
            throw new \InvalidArgumentException('a year day value is not valid');
53
        }
54
    }
55
56
    /**
57
     * @return void
58
     */
59
    private function validateState()
60
    {
61
        $this->validateDay();
62
        $this->validateMonth();
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    private function validateDay()
69
    {
70
        if (null !== $this->day && (1 > $this->day || $this->getMaxDays() < $this->day)) {
71
            throw new \InvalidArgumentException('the day must be greater 0 and less then 32');
72
        }
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    private function getMaxDays()
79
    {
80
        if (null !== $this->month) {
81
            return (string)cal_days_in_month(CAL_GREGORIAN, $this->month, $this->getYearOrHolder());
82
        }
83
        return '31';
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    private function getYearOrHolder()
90
    {
91
        if (null === $this->year) {
92
            return '2016';
93
        }
94
        return $this->year;
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    private function validateMonth()
101
    {
102
        if (null !== $this->month && (1 > $this->month || 12 < $this->month)) {
103
            throw new \InvalidArgumentException('the month must be greater 0 and less then 12');
104
        }
105
    }
106
}
107