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
|
|
|
private function validateInput($year = null, $month = null, $day = null) |
27
|
|
|
{ |
28
|
|
View Code Duplication |
if (null === $year && null === $month && null === $day) { |
|
|
|
|
29
|
|
|
throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year'); |
30
|
|
|
} |
31
|
|
View Code Duplication |
if (null !== $year && null === $month && null !== $day) { |
|
|
|
|
32
|
|
|
throw new \InvalidArgumentException('a year day value is not valid'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function valdidateState() |
37
|
|
|
{ |
38
|
|
|
if (null !== $this->day && (1 > $this->day || 31 < $this->day)) { |
39
|
|
|
throw new \InvalidArgumentException('the day must be greater 0 and less then 32'); |
40
|
|
|
} |
41
|
|
|
if (null !== $this->month && (1 > $this->month || 12 < $this->month)) { |
42
|
|
|
throw new \InvalidArgumentException('the month must be greater 0 and less then 12'); |
43
|
|
|
} |
44
|
|
|
if ($this->month !== null && $this->day !== null && |
45
|
|
|
cal_days_in_month($this->getYearOrHolder(), $this->month, $this->day) < $this->day |
46
|
|
|
) { |
47
|
|
|
throw new \InvalidArgumentException('there are not that many days in the assigned month'); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getYearOrHolder() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
if (null == $this->year) { |
54
|
|
|
return '2016'; |
55
|
|
|
} |
56
|
|
|
return $this->year; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $day should match regex pattern ----\d\d |
61
|
|
|
* |
62
|
|
|
* @return Calender |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public static function fromDay($day) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$re = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
67
|
|
|
preg_match_all($re, $day, $matches, PREG_SET_ORDER, 0); |
68
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
69
|
|
|
throw new \InvalidArgumentException('Unable to extract day from input string'); |
70
|
|
|
} |
71
|
|
|
return new self(null, null, $matches[0][1], $matches[0][2]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public static function FromMonthDay($monthDay) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$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*)/'; |
77
|
|
|
preg_match_all($re, $monthDay, $matches, PREG_SET_ORDER, 0); |
78
|
|
|
if (count($matches) != 1 && count($matches[0]) != 4) { |
79
|
|
|
throw new \InvalidArgumentException('Unable to extract month day from input string'); |
80
|
|
|
} |
81
|
|
|
return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public static function fromMonth($month) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$re = '/--(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
87
|
|
|
preg_match_all($re, $month, $matches, PREG_SET_ORDER, 0); |
88
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
89
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
90
|
|
|
} |
91
|
|
|
return new self(null, $matches[0][1], null, $matches[0][2]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public static function fromYearMonth($yearMonth) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$re = '/(\d{4})-(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/'; |
97
|
|
|
preg_match_all($re, $yearMonth, $matches, PREG_SET_ORDER, 0); |
98
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
99
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
100
|
|
|
} |
101
|
|
|
return new self($matches[0][1], null, $matches[0][2], $matches[0][3]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public static function fromYear($year) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$re = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/'; |
107
|
|
|
preg_match_all($re, $year, $matches, PREG_SET_ORDER, 0); |
108
|
|
|
if (count($matches) != 1 && count($matches[0]) != 3) { |
109
|
|
|
throw new \InvalidArgumentException('Unable to extract month from input string'); |
110
|
|
|
} |
111
|
|
|
return new self($matches[0][1], null, null, $matches[0][2]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|