|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stefanius\SpecialDates\SDK; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AbstractSpecialDate implements SpecialDateInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var \DateTime |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $startDate; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \DateTime |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $endDate; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \DateTime |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $zeroDate; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $totalLength = 0; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $valid = true; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $bankHoliday = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $nationalAcceptedParty = false; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $normalizedDescription = false; |
|
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $year; |
|
55
|
|
|
|
|
56
|
68 |
|
public function __construct($year) |
|
57
|
|
|
{ |
|
58
|
68 |
|
$this->year = $year; |
|
59
|
68 |
|
$this->zeroDate = new \DateTime(); |
|
60
|
68 |
|
$this->zeroDate->setDate(1, 1, 1); |
|
61
|
|
|
|
|
62
|
68 |
|
$this->startDate = new \DateTime(); |
|
63
|
68 |
|
$this->startDate->setDate(1, 1, 1); |
|
64
|
|
|
|
|
65
|
68 |
|
$this->endDate = new \DateTime(); |
|
66
|
68 |
|
$this->endDate->setDate(1, 1, 1); |
|
67
|
|
|
|
|
68
|
68 |
|
$this->generate(); |
|
69
|
|
|
|
|
70
|
65 |
View Code Duplication |
if ($this->startDate->format('Y') . '-' . $this->startDate->format('m') . '-' . $this->startDate->format('d') === '0001-01-01') { |
|
|
|
|
|
|
71
|
27 |
|
$this->valid = false; |
|
72
|
27 |
|
$this->totalLength = 0; |
|
73
|
27 |
|
} |
|
74
|
|
|
|
|
75
|
65 |
View Code Duplication |
if ($this->endDate->format('Y') . '-' . $this->endDate->format('m') . '-' . $this->endDate->format('d') === '0001-01-01') { |
|
|
|
|
|
|
76
|
27 |
|
$this->valid = false; |
|
77
|
27 |
|
$this->totalLength = 0; |
|
78
|
27 |
|
} |
|
79
|
|
|
|
|
80
|
65 |
|
$this->normalizeDescription(); |
|
81
|
65 |
|
} |
|
82
|
|
|
|
|
83
|
65 |
|
protected function normalizeDescription() |
|
84
|
|
|
{ |
|
85
|
65 |
|
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $this->description); |
|
86
|
65 |
|
$string = trim($string); |
|
87
|
65 |
|
$string = preg_replace("/[^a-zA-Z0-9_| -]/", ' ', $string); |
|
88
|
65 |
|
$string = preg_replace("/[| -]+/", '-', $string); |
|
89
|
65 |
|
$string = strtolower(trim($string, '-')); |
|
90
|
65 |
|
$string = preg_replace('/-{2,}/', ' ', $string); |
|
91
|
|
|
|
|
92
|
65 |
|
$this->normalizedDescription = $string; |
|
93
|
65 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $year |
|
97
|
|
|
* @param $month |
|
98
|
|
|
* @param $day |
|
99
|
|
|
* |
|
100
|
|
|
* @return \DateTime |
|
101
|
|
|
*/ |
|
102
|
15 |
|
protected function generateDateTime($year, $month, $day) |
|
103
|
|
|
{ |
|
104
|
15 |
|
$date = \DateTime::createFromFormat('Y-m-d', $year . '-' . $month . '-' . $day); |
|
105
|
|
|
|
|
106
|
15 |
|
if ($date === false) { |
|
107
|
|
|
return $this->zeroDate; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
15 |
|
return $date; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
21 |
|
protected function setupDateTimeObjects(\DateTime $start, \DateTime $end = null) |
|
114
|
|
|
{ |
|
115
|
21 |
|
$this->startDate = $start; |
|
116
|
|
|
|
|
117
|
21 |
|
if ($end === null) { |
|
118
|
17 |
|
$this->endDate = $start; |
|
119
|
17 |
|
} else { |
|
120
|
4 |
|
$this->endDate = $end; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
21 |
|
$interval = $this->startDate->diff($this->endDate); |
|
124
|
|
|
|
|
125
|
21 |
|
$this->totalLength = (int)$interval->format('%R%a') + 1; |
|
126
|
21 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return \DateTime |
|
130
|
|
|
*/ |
|
131
|
63 |
|
public function getStartDate() |
|
132
|
|
|
{ |
|
133
|
63 |
|
if (!$this->valid || $this->startDate == null) { |
|
134
|
25 |
|
return $this->zeroDate; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
38 |
|
return $this->startDate; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return \DateTime |
|
142
|
|
|
*/ |
|
143
|
63 |
|
public function getEndDate() |
|
144
|
|
|
{ |
|
145
|
63 |
|
if (!$this->valid || $this->endDate == null) { |
|
146
|
25 |
|
return $this->zeroDate; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
38 |
|
return $this->endDate; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
63 |
|
public function getDescription() |
|
156
|
|
|
{ |
|
157
|
63 |
|
return $this->description; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return int |
|
162
|
|
|
*/ |
|
163
|
63 |
|
public function getTotalLength() |
|
164
|
|
|
{ |
|
165
|
63 |
|
return $this->totalLength; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
63 |
|
public function isValid() |
|
172
|
|
|
{ |
|
173
|
63 |
|
return $this->valid; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return boolean |
|
178
|
|
|
*/ |
|
179
|
63 |
|
public function isBankHoliday() |
|
180
|
|
|
{ |
|
181
|
63 |
|
return $this->bankHoliday; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return boolean |
|
186
|
|
|
*/ |
|
187
|
|
|
public function isNationalAcceptedParty() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->nationalAcceptedParty; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return int |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getYear() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->year; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getNormalizedDescription() |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->normalizedDescription; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
abstract protected function generate(); |
|
209
|
|
|
} |
|
210
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.