|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Barnso |
|
5
|
|
|
* Date: 14/07/2017 |
|
6
|
|
|
* Time: 8:28 PM. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace AlgoWeb\xsdTypes\AxillaryClasses; |
|
9
|
|
|
|
|
10
|
|
|
class Calender |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
const timezoneRegexPattern = '([-+][0-1]\d:[0-6]\d|Z*)'; |
|
|
|
|
|
|
13
|
|
|
private $year; |
|
14
|
|
|
private $month; |
|
15
|
|
|
private $day; |
|
16
|
|
|
private $timeZone; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Calender constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param int|null $year |
|
22
|
|
|
* @param int|null $month |
|
23
|
|
|
* @param int|null $day |
|
24
|
|
|
* @param string|null $timezone |
|
25
|
|
|
*/ |
|
26
|
|
|
private function __construct($year = null, $month = null, $day = null, $timezone = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->validateInput($year, $month, $day); |
|
29
|
|
|
$this->year = $year; |
|
30
|
|
|
$this->month = $month; |
|
31
|
|
|
$this->day = $day; |
|
32
|
|
|
$this->timeZone = $timezone; |
|
33
|
|
|
$this->valdidateState(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param int $year |null |
|
|
|
|
|
|
38
|
|
|
* @param int $month |null |
|
|
|
|
|
|
39
|
|
|
* @param int $day |null |
|
|
|
|
|
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
private function validateInput($year = null, $month = null, $day = null) |
|
44
|
|
|
{ |
|
45
|
|
View Code Duplication |
if (null === $year && null === $month && null === $day) { |
|
46
|
|
|
throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year'); |
|
47
|
|
|
} |
|
48
|
|
View Code Duplication |
if (null !== $year && null === $month && null !== $day) { |
|
49
|
|
|
throw new \InvalidArgumentException('a year day value is not valid'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
|
|
private function valdidateState() |
|
57
|
|
|
{ |
|
58
|
|
|
if (null !== $this->day && (1 > $this->day || $this->getMaxDays() < $this->day)) { |
|
59
|
|
|
throw new \InvalidArgumentException('the day must be greater 0 and less then 32'); |
|
60
|
|
|
} |
|
61
|
|
|
if (null !== $this->month && (1 > $this->month || 12 < $this->month)) { |
|
62
|
|
|
throw new \InvalidArgumentException('the month must be greater 0 and less then 12'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return integer |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getMaxDays() |
|
70
|
|
|
{ |
|
71
|
|
|
if (null !== $this->month) { |
|
72
|
|
|
return cal_days_in_month(CAL_GREGORIAN, $this->month, $this->getYearOrHolder()); |
|
73
|
|
|
} |
|
74
|
|
|
return 31; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return integer |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
|
|
private function getYearOrHolder() |
|
81
|
|
|
{ |
|
82
|
|
|
if (null == $this->year) { |
|
|
|
|
|
|
83
|
|
|
return '2016'; |
|
84
|
|
|
} |
|
85
|
|
|
return $this->year; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $day should match regex pattern ----\d\d |
|
90
|
|
|
* |
|
91
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public static function fromDay($day) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$re = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
|
96
|
|
|
preg_match_all($re, $day, $matches, PREG_SET_ORDER, 0); |
|
97
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
|
98
|
|
|
throw new \InvalidArgumentException('Unable to extract day from input string'); |
|
99
|
|
|
} |
|
100
|
|
|
return new self(null, null, $matches[0][1], $matches[0][2]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $monthDay |
|
105
|
|
|
* |
|
106
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
public static function fromMonthDay($monthDay) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$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*)/'; |
|
111
|
|
|
preg_match_all($re, $monthDay, $matches, PREG_SET_ORDER, 0); |
|
112
|
|
|
if (count($matches) != 1 && count($matches[0]) != 4) { |
|
113
|
|
|
throw new \InvalidArgumentException('Unable to extract month day from input string'); |
|
114
|
|
|
} |
|
115
|
|
|
return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $month |
|
120
|
|
|
* |
|
121
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public static function fromMonth($month) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$re = '/--(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
|
126
|
|
|
preg_match_all($re, $month, $matches, PREG_SET_ORDER, 0); |
|
127
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
|
128
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
|
129
|
|
|
} |
|
130
|
|
|
return new self(null, $matches[0][1], null, $matches[0][2]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $yearMonth |
|
135
|
|
|
* |
|
136
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
|
137
|
|
|
*/ |
|
138
|
|
View Code Duplication |
public static function fromYearMonth($yearMonth) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$re = '/(\d{4})-(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
|
141
|
|
|
preg_match_all($re, $yearMonth, $matches, PREG_SET_ORDER, 0); |
|
142
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
|
143
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
|
144
|
|
|
} |
|
145
|
|
|
return new self($matches[0][1], null, $matches[0][2], $matches[0][3]); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $year |
|
150
|
|
|
* |
|
151
|
|
|
* @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
|
152
|
|
|
*/ |
|
153
|
|
View Code Duplication |
public static function fromYear($year) |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
$re = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/'; |
|
156
|
|
|
preg_match_all($re, $year, $matches, PREG_SET_ORDER, 0); |
|
157
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
|
158
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
|
159
|
|
|
} |
|
160
|
|
|
return new self($matches[0][1], null, null, $matches[0][2]); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|