-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Event.php
297 lines (253 loc) · 7.3 KB
/
Event.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Mzur\KirbyCalendar;
use Exception;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
class Event {
/**
* The string for the beginning date field key of an event.
*/
const BEGIN_DATE_KEY = '_begin_date';
/**
* The string for the beginning time field key of an event.
*/
const BEGIN_TIME_KEY = '_begin_time';
/**
* The string for the end date field key of an event.
*/
const END_DATE_KEY = '_end_date';
/**
* The string for the end time field key of an event.
*/
const END_TIME_KEY = '_end_time';
/**
* An array of field keys that are required to create an event.
*/
private static $requiredKeys = [
self::BEGIN_DATE_KEY,
];
/**
* The timestamp of the beginning of this event.
*/
private $beginTimestamp;
/**
* The timestamp of the end of this event. May be false if the event only
* lasts a day.
*/
private $endTimestamp;
/**
* Was an ending date given?
*/
private $hasEnd;
/**
* Was a beginning time given for this event?
*/
private $hasBeginTime;
/**
* Was an ending time given for this event?
*/
private $hasEndTime;
/**
* Formatting of the output string of beginning and end times.
*/
private $timeFormat;
/**
* Array of fields without the 'private' fields starting with a
* <code>_</code>.
*/
private $fields;
/**
* @param array $event The fields of this event including the 'private'
* fields which start with a <code>_</code> (e.g. <code>_begin_date</code>).
*/
function __construct($event) {
self::validate($event);
$this->hasEnd = true;
$this->hasBeginTime = (bool) A::get($event, self::BEGIN_TIME_KEY);
$this->hasEndTime = (bool) A::get($event, self::END_TIME_KEY);
$this->beginTimestamp = self::getDate(
A::get($event, self::BEGIN_DATE_KEY),
A::get($event, self::BEGIN_TIME_KEY)
);
$this->endTimestamp = self::getDate(
A::get($event, self::END_DATE_KEY),
A::get($event, self::END_TIME_KEY)
);
// if there is no end date given, use the same as the beginning date
if (!$this->endTimestamp) {
$this->endTimestamp = self::getDate(
A::get($event, self::BEGIN_DATE_KEY),
A::get($event, self::END_TIME_KEY)
);
// if there also is no end time given, there is no end at all
if (!$this->hasEndTime) {
$this->hasEnd = false;
}
}
// if there is no end time given, the event lasts until end of the day
if (!$this->hasEndTime) {
$this->endTimestamp->setTime(23, 59, 59);
}
// only use the full format, if there were times given for this event
$this->timeFormat = ($this->hasBeginTime || $this->hasEndTime)
? t('calendar-full-time-format')
: t('calendar-time-format');
// remove the 'private' fields
$this->fields = self::filterFields($event);
}
/**
* @param array $event A 'raw' event array.
* @return A new Event instance.
*/
public static function instantiate($event) {
return new Event($event);
}
/**
* @param Event $e1
* @param Event $e2
* @return Integer < 0 if $e1 is older than $e2, 0 if they happen at the
* same time and > 0 if $e2 is older than $e1
*/
public static function compare($e1, $e2) {
return $e1->beginTimestamp <=> $e2->beginTimestamp;
}
/**
* @param Event $e
* @return <code>false</code> if the event is past, <code>true</code>
* otherwise.
*/
public static function filterPast($e) {
return !$e->isPast();
}
/**
* Checks if all required keys are in the 'raw' event array. Throws an
* exception if one is missing.
*
* @param array $event a 'raw' event array containing all fields
*/
private static function validate($event) {
$missingKeys = A::missing($event, self::$requiredKeys);
if ($missingKeys) {
$message = "Event creation failed because of the following missing " .
"required fields:\n" . A::show($missingKeys, false);
throw new Exception($message, 1);
}
}
/**
* @param string $date the date, e.g. '01.01.1970'
* @param string $time optional time, e.g. '10:00:00'
* @return The date as a DateTimeImmutable object or <code>false</code> if there
* was no $date given.
*/
private static function getDate($date, $time = '') {
if ($date) {
return new \DateTimeImmutable($date . ' ' . $time);
} else {
return false;
}
}
/**
* @param array $event the 'raw' event array of fields.
* @return the array of fields without the 'private' fields with keys
*/
private static function filterFields($event) {
foreach (array_keys($event) as $key) {
if (Str::startsWith($key, '_')) {
unset($event[$key]);
}
}
return $event;
}
/**
* @return The timestamp in seconds for the beginning of this event.
*/
public function getBeginTimestamp() {
return $this->beginTimestamp->getTimestamp();
}
/**
* @return The date array of the beginning of this event.
*/
public function getBeginDate() {
return getdate($this->beginTimestamp->getTimestamp());
}
/**
* @param string $languageCode the language used to create the date string, e.g. 'de'
* @return The formatted string of the beginning of this event. Formatting
* is done according to the language code given as argument.
*/
public function getBeginStr($languageCode) {
return \IntlDateFormatter::formatObject(
$this->beginTimestamp,
$this->timeFormat,
$languageCode);
}
/**
* @return The formatted string of the beginning of this event wrapped in
* a <code>time</code> element with <code>datetime</code> attribute.
*/
public function getBeginHtml($languageCode = 'en') {
return '<time datetime="' .
gmdate('Y-m-d\TH:i:s\Z', $this->beginTimestamp->getTimestamp()) . '">' .
$this->getBeginStr($languageCode) . '</time>';
}
/**
* @return The timestamp in seconds for the ending of this event.
*/
public function getEndTimestamp() {
return $this->endTimestamp->getTimestamp();
}
/**
* @return The date array of the ending of this event.
*/
public function getEndDate() {
return getdate($this->endTimestamp->getTimestamp());
}
/**
* @param string $languageCode the language used to create the date string, e.g. 'de'
* @return The formatted string of the ending of this event. Formatting
* is done according to the language code given as argument.
*/
public function getEndStr($languageCode) {
return \IntlDateFormatter::formatObject(
$this->endTimestamp,
$this->timeFormat,
$languageCode);
}
/**
* @return The formatted string of the ending of this event wrapped in
* a <code>time</code> element with <code>datetime</code> attribute.
*/
public function getEndHtml($languageCode = 'en') {
return '<time datetime="' .
gmdate('Y-m-d\TH:i:s\Z', $this->endTimestamp->getTimestamp()) . '">' .
$this->getEndStr($languageCode) . '</time>';
}
/**
* @return All non-'private' field keys of this event.
*/
public function getFieldKeys() {
return array_keys($this->fields);
}
/**
* @param string $key The key of the field to get.
* @return The content of the field or an empty string if it doesn't exist
* in this event.
*/
public function getField($key) {
return A::get($this->fields, $key, '');
}
/**
* @return <code>true</code> if the event is past at the current time,
* <code>false</code> otherwise
*/
public function isPast() {
return $this->endTimestamp < new \DateTime("now");
}
/**
* @return <code>true</code> if the event has an ending date/time
* <code>false</code> otherwise
*/
public function hasEnd() {
return $this->hasEnd;
}
}