1
|
|
|
<?php |
2
|
|
|
namespace AlgoWeb\xsdTypes\AxillaryClasses; |
3
|
|
|
|
4
|
|
|
final class Calender |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
private $year; |
8
|
|
|
private $month; |
9
|
|
|
private $day; |
10
|
|
|
private $timeZone; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Calender constructor. |
14
|
|
|
* |
15
|
|
|
* @param string|null $year |
16
|
|
|
* @param string|null $month |
17
|
|
|
* @param string|null $day |
18
|
|
|
* @param string|null $timezone |
19
|
|
|
*/ |
20
|
|
|
private function __construct($year = null, $month = null, $day = null, $timezone = null) |
21
|
|
|
{ |
22
|
|
|
$this->validateInput($year, $month, $day); |
23
|
|
|
$this->year = $year; |
24
|
|
|
$this->month = $month; |
25
|
|
|
$this->day = $day; |
26
|
|
|
$this->timeZone = $timezone; |
27
|
|
|
$this->validateState(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string|null $year |
32
|
|
|
* @param string|null $month |
33
|
|
|
* @param string|null $day |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
private function validateInput($year = null, $month = null, $day = null) |
38
|
|
|
{ |
39
|
|
View Code Duplication |
if (null === $year && null === $month && null === $day) { |
40
|
|
|
throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year'); |
41
|
|
|
} |
42
|
|
View Code Duplication |
if (null !== $year && null === $month && null !== $day) { |
43
|
|
|
throw new \InvalidArgumentException('a year day value is not valid'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
private function validateState() |
51
|
|
|
{ |
52
|
|
|
$this->validateDay(); |
53
|
|
|
$this->validateMonth(); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
private function validateDay() |
61
|
|
|
{ |
62
|
|
|
if (null !== $this->day && (1 > $this->day || $this->getMaxDays() < $this->day)) { |
63
|
|
|
throw new \InvalidArgumentException('the day must be greater 0 and less then 32'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
private function getMaxDays() |
71
|
|
|
{ |
72
|
|
|
if (null !== $this->month) { |
73
|
|
|
return (string)cal_days_in_month(CAL_GREGORIAN, $this->month, $this->getYearOrHolder()); |
74
|
|
|
} |
75
|
|
|
return '31'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
private function getYearOrHolder() |
82
|
|
|
{ |
83
|
|
|
if (null === $this->year) { |
84
|
|
|
return '2016'; |
85
|
|
|
} |
86
|
|
|
return $this->year; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
private function validateMonth() |
93
|
|
|
{ |
94
|
|
|
if (null !== $this->month && (1 > $this->month || 12 < $this->month)) { |
95
|
|
|
throw new \InvalidArgumentException('the month must be greater 0 and less then 12'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $day should match regex pattern ----\d\d |
101
|
|
|
* |
102
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public static function fromDay($day) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$re = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
107
|
|
|
preg_match_all($re, $day, $matches, PREG_SET_ORDER, 0); |
108
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
109
|
|
|
throw new \InvalidArgumentException('Unable to extract day from input string'); |
110
|
|
|
} |
111
|
|
|
return new self(null, null, $matches[0][1], $matches[0][2]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $monthDay |
116
|
|
|
* |
117
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public static function fromMonthDay($monthDay) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$re = '/--(1[0-2]|0[1-9]|[1-9])-(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
122
|
|
|
preg_match_all($re, $monthDay, $matches, PREG_SET_ORDER, 0); |
123
|
|
|
if (count($matches) != 1 && count($matches[0]) != 4) { |
124
|
|
|
throw new \InvalidArgumentException('Unable to extract month day from input string'); |
125
|
|
|
} |
126
|
|
|
return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $month |
131
|
|
|
* |
132
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public static function fromMonth($month) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$re = '/--(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
137
|
|
|
preg_match_all($re, $month, $matches, PREG_SET_ORDER, 0); |
138
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
139
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
140
|
|
|
} |
141
|
|
|
return new self(null, $matches[0][1], null, $matches[0][2]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $yearMonth |
146
|
|
|
* |
147
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public static function fromYearMonth($yearMonth) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$re = '/(\d{4})-(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
152
|
|
|
preg_match_all($re, $yearMonth, $matches, PREG_SET_ORDER, 0); |
153
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
154
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
155
|
|
|
} |
156
|
|
|
return new self($matches[0][1], null, $matches[0][2], $matches[0][3]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $year |
161
|
|
|
* |
162
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public static function fromYear($year) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$re = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/'; |
167
|
|
|
preg_match_all($re, $year, $matches, PREG_SET_ORDER, 0); |
168
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
169
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
170
|
|
|
} |
171
|
|
|
return new self($matches[0][1], null, null, $matches[0][2]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.