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
|
69 |
|
public function __construct($year) |
57
|
|
|
{ |
58
|
69 |
|
$this->year = $year; |
59
|
69 |
|
$this->zeroDate = new \DateTime(); |
60
|
69 |
|
$this->zeroDate->setDate(1, 1, 1); |
61
|
|
|
|
62
|
69 |
|
$this->startDate = new \DateTime(); |
63
|
69 |
|
$this->startDate->setDate(1, 1, 1); |
64
|
|
|
|
65
|
69 |
|
$this->endDate = new \DateTime(); |
66
|
69 |
|
$this->endDate->setDate(1, 1, 1); |
67
|
|
|
|
68
|
69 |
|
$this->generate(); |
69
|
|
|
|
70
|
69 |
View Code Duplication |
if ($this->startDate->format('Y') . '-' . $this->startDate->format('m') . '-' . $this->startDate->format('d') === '0001-01-01') { |
|
|
|
|
71
|
28 |
|
$this->valid = false; |
72
|
28 |
|
$this->totalLength = 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
69 |
View Code Duplication |
if ($this->endDate->format('Y') . '-' . $this->endDate->format('m') . '-' . $this->endDate->format('d') === '0001-01-01') { |
|
|
|
|
76
|
28 |
|
$this->valid = false; |
77
|
28 |
|
$this->totalLength = 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
69 |
|
$this->normalizeDescription(); |
81
|
69 |
|
} |
82
|
|
|
|
83
|
69 |
|
protected function normalizeDescription() |
84
|
|
|
{ |
85
|
69 |
|
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $this->description); |
86
|
69 |
|
$string = trim($string); |
87
|
69 |
|
$string = preg_replace("/[^a-zA-Z0-9_| -]/", ' ', $string); |
88
|
69 |
|
$string = preg_replace("/[| -]+/", '-', $string); |
89
|
69 |
|
$string = strtolower(trim($string, '-')); |
90
|
69 |
|
$string = preg_replace('/-{2,}/', ' ', $string); |
91
|
|
|
|
92
|
69 |
|
$this->normalizedDescription = $string; |
93
|
69 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $year |
97
|
|
|
* @param $month |
98
|
|
|
* @param $day |
99
|
|
|
* |
100
|
|
|
* @return \DateTime |
101
|
|
|
*/ |
102
|
19 |
|
protected function generateDateTime($year, $month, $day) |
103
|
|
|
{ |
104
|
19 |
|
$date = \DateTime::createFromFormat('Y-m-d', $year . '-' . $month . '-' . $day); |
105
|
|
|
|
106
|
19 |
|
if ($date === false) { |
107
|
|
|
return $this->zeroDate; |
108
|
|
|
} |
109
|
|
|
|
110
|
19 |
|
return $date; |
111
|
|
|
} |
112
|
|
|
|
113
|
25 |
|
protected function setupDateTimeObjects(\DateTime $start, \DateTime $end = null) |
114
|
|
|
{ |
115
|
25 |
|
$this->startDate = $start; |
116
|
|
|
|
117
|
25 |
|
if ($end === null) { |
118
|
21 |
|
$this->endDate = $start; |
119
|
|
|
} else { |
120
|
4 |
|
$this->endDate = $end; |
121
|
|
|
} |
122
|
|
|
|
123
|
25 |
|
$interval = $this->startDate->diff($this->endDate); |
124
|
|
|
|
125
|
25 |
|
$this->totalLength = (int)$interval->format('%R%a') + 1; |
126
|
25 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return \DateTime |
130
|
|
|
*/ |
131
|
69 |
|
public function getStartDate() |
132
|
|
|
{ |
133
|
69 |
|
if (!$this->valid || $this->startDate == null) { |
134
|
28 |
|
return $this->zeroDate; |
135
|
|
|
} |
136
|
|
|
|
137
|
44 |
|
return $this->startDate; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return \DateTime |
142
|
|
|
*/ |
143
|
66 |
|
public function getEndDate() |
144
|
|
|
{ |
145
|
66 |
|
if (!$this->valid || $this->endDate == null) { |
146
|
25 |
|
return $this->zeroDate; |
147
|
|
|
} |
148
|
|
|
|
149
|
41 |
|
return $this->endDate; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
66 |
|
public function getDescription() |
156
|
|
|
{ |
157
|
66 |
|
return $this->description; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
66 |
|
public function getTotalLength() |
164
|
|
|
{ |
165
|
66 |
|
return $this->totalLength; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
66 |
|
public function isValid() |
172
|
|
|
{ |
173
|
66 |
|
return $this->valid; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return boolean |
178
|
|
|
*/ |
179
|
66 |
|
public function isBankHoliday() |
180
|
|
|
{ |
181
|
66 |
|
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
|
1 |
|
public function getNormalizedDescription() |
204
|
|
|
{ |
205
|
1 |
|
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.