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