1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class AbstractEvent |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Event; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Event\EventInterface; |
11
|
|
|
use Roomify\Bat\Store\Store; |
12
|
|
|
use Roomify\Bat\EventFormatter\EventFormatter; |
13
|
|
|
use Roomify\Bat\Store\StoreInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractEvent implements EventInterface { |
16
|
|
|
|
17
|
|
|
const BAT_DAY = 'bat_day'; |
18
|
|
|
const BAT_HOUR = 'bat_hour'; |
19
|
|
|
const BAT_MINUTE = 'bat_minute'; |
20
|
|
|
const BAT_HOURLY = 'bat_hourly'; |
21
|
|
|
const BAT_DAILY = 'bat_daily'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The booking unit the event is relevant to |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $unit_id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The unit the event is relevant to |
31
|
|
|
*/ |
32
|
|
|
protected $unit; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The start date for the event. |
36
|
|
|
* |
37
|
|
|
* @var \DateTime |
38
|
|
|
*/ |
39
|
|
|
protected $start_date; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The end date for the event. Keep in mind that BAT considers a time such as |
43
|
|
|
* 1358 to include the entire 58th minute. So what an event that we would describe |
44
|
|
|
* as starting at 1300 and ending at 1400 for BAT actually ends at 1359. This is because |
45
|
|
|
* (among other reasons) there may well be another event starting at 1400 and two events |
46
|
|
|
* next to each other cannot share the same time. |
47
|
|
|
* |
48
|
|
|
* @var \DateTime |
49
|
|
|
*/ |
50
|
|
|
protected $end_date; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The value associated with this event. |
54
|
|
|
* This can represent an availability state or a pricing value |
55
|
|
|
* |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
protected $value; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the value. |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
public function getValue() { |
66
|
|
|
return (int) $this->value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the value. |
71
|
|
|
* |
72
|
|
|
* @param int $value |
73
|
|
|
*/ |
74
|
|
|
public function setValue($value) { |
75
|
|
|
$this->value = $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the unit id. |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
public function getUnitId() { |
84
|
|
|
return $this->unit_id; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the unit id. |
89
|
|
|
* |
90
|
|
|
* @param int $unit_id |
91
|
|
|
*/ |
92
|
|
|
public function setUnitId($unit_id) { |
93
|
|
|
$this->unit_id = $unit_id; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the start date. |
98
|
|
|
* |
99
|
|
|
* @return \DateTime |
100
|
|
|
*/ |
101
|
|
|
public function getStartDate() { |
102
|
|
|
return clone($this->start_date); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Utility function to always give us a standard format for viewing the start date. |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function startDateToString($format = 'Y-m-d H:i') { |
110
|
|
|
return $this->start_date->format($format); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the start date. |
115
|
|
|
* |
116
|
|
|
* @param \DateTime $start_date |
117
|
|
|
*/ |
118
|
|
|
public function setStartDate(\DateTime $start_date) { |
119
|
|
|
$this->start_date = clone($start_date); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the end date. |
124
|
|
|
* |
125
|
|
|
* @return \DateTime |
126
|
|
|
*/ |
127
|
|
|
public function getEndDate() { |
128
|
|
|
return clone($this->end_date); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Utility function to always give us a standard format for viewing the end date. |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function endDateToString($format = 'Y-m-d H:i') { |
136
|
|
|
return $this->end_date->format($format); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the end date. |
141
|
|
|
* |
142
|
|
|
* @param \DateTime $end_date |
143
|
|
|
*/ |
144
|
|
|
public function setEndDate(\DateTime $end_date) { |
145
|
|
|
$this->end_date = clone($end_date); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function startDay($format = 'j') { |
152
|
|
|
return $this->start_date->format($format); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function endDay($format = 'j') { |
159
|
|
|
return $this->end_date->format($format); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function startMonth($format = 'n') { |
166
|
|
|
return $this->start_date->format($format); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function endMonth($format = 'n') { |
173
|
|
|
return $this->end_date->format($format); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
*{@inheritdoc) |
178
|
|
|
*/ |
179
|
|
|
public function endMonthDate(\DateTime $date) { |
180
|
|
|
// The time is added so that the end date is included |
181
|
|
|
$date_format = $date->format('Y-n-t 23:59:59'); |
182
|
|
|
return new \DateTime($date_format); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function startYear($format = 'Y') { |
189
|
|
|
return $this->start_date->format($format); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
public function endYear($format = 'Y') { |
196
|
|
|
return $this->end_date->format($format); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function startWeek($format = 'W') { |
203
|
|
|
return $this->start_date->format($format); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
|
|
*/ |
209
|
|
|
public function endWeek($format = 'W') { |
210
|
|
|
return $this->end_date->format($format); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
|
|
public function startHour($format = 'H') { |
217
|
|
|
return $this->start_date->format($format); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function endHour($format = 'H') { |
224
|
|
|
return $this->end_date->format($format); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function startMinute($format = 'i') { |
231
|
|
|
return $this->start_date->format($format); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* {@inheritdoc} |
236
|
|
|
*/ |
237
|
|
|
public function endMinute($format = 'i') { |
238
|
|
|
return $this->end_date->format($format); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
|
|
public function isFirstMonth($date) { |
245
|
|
|
if ($date->format("n") == $this->startMonth() && $date->format("Y") == $this->startYear()) { |
246
|
|
|
return TRUE; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return FALSE; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
|
|
public function isLastMonth($date) { |
256
|
|
|
if ($date->format("n") == $this->endMonth() && $date->format("Y") == $this->endYear()) { |
257
|
|
|
return TRUE; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return FALSE; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* {@inheritdoc} |
265
|
|
|
*/ |
266
|
|
|
public function isFirstDay($date) { |
267
|
|
|
if (($date->format('j') == $this->startDay()) && ($this->isFirstMonth($date))) { |
268
|
|
|
return TRUE; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return FALSE; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
|
|
public function isFirstHour($date) { |
278
|
|
|
if ($date->format('G') == $this->startHour() && $this->isFirstDay($date)) { |
279
|
|
|
return TRUE; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return FALSE; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* {@inheritdoc} |
287
|
|
|
*/ |
288
|
|
|
public function isSameYear() { |
289
|
|
|
if ($this->startYear() == $this->endYear()) { |
290
|
|
|
return TRUE; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return FALSE; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* {@inheritdoc} |
298
|
|
|
*/ |
299
|
|
|
public function isSameMonth() { |
300
|
|
|
if (($this->startMonth() == $this->endMonth()) && $this->isSameYear()) { |
301
|
|
|
return TRUE; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return FALSE; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* {@inheritdoc} |
309
|
|
|
*/ |
310
|
|
|
public function isSameDay() { |
311
|
|
|
if (($this->startDay() == $this->endDay()) && $this->isSameMonth()) { |
312
|
|
|
return TRUE; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return FALSE; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* {@inheritdoc} |
320
|
|
|
*/ |
321
|
|
|
public function isSameHour() { |
322
|
|
|
if (($this->startHour() == $this->endHour()) && $this->isSameDay()) { |
323
|
|
|
return TRUE; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return FALSE; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* {@inheritdoc} |
331
|
|
|
*/ |
332
|
|
|
public function diff() { |
333
|
|
|
$interval = $this->start_date->diff($this->end_date); |
334
|
|
|
return $interval; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Returns true if the event overlaps at all with the time period within |
339
|
|
|
* the start and end time. |
340
|
|
|
* |
341
|
|
|
* @param \DateTime $start |
342
|
|
|
* @param \DateTime $end |
343
|
|
|
* @return bool |
344
|
|
|
*/ |
345
|
|
|
public function overlaps(\DateTime $start, \DateTime $end) { |
346
|
|
|
$overlaps = FALSE; |
347
|
|
|
|
348
|
|
|
if ($this->dateIsEarlier($start) && |
349
|
|
|
($this->dateIsInRange($end) || $this->dateIsLater($end))) { |
350
|
|
|
$overlaps = TRUE; |
351
|
|
|
} elseif ($this->dateIsInRange($start) && |
352
|
|
|
($this->dateIsInRange($end) || $this->dateIsLater($end))) { |
353
|
|
|
$overlaps = TRUE; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $overlaps; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Checks if date supplied is in range of event |
361
|
|
|
* |
362
|
|
|
* @param \DateTime $date |
363
|
|
|
* @return bool |
364
|
|
|
*/ |
365
|
|
|
public function dateIsInRange(\DateTime $date) { |
366
|
|
|
$dateInRange = FALSE; |
367
|
|
|
|
368
|
|
|
$t1 = $this->start_date->getTimeStamp(); |
369
|
|
|
$t2 = $this->end_date->getTimeStamp(); |
370
|
|
|
|
371
|
|
|
$t3 = $date->getTimeStamp(); |
372
|
|
|
|
373
|
|
|
if (($t3 >= $t1) && ($t3 <= $t2)) { |
374
|
|
|
$dateInRange = TRUE; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return $dateInRange; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Checks if the date supplied starts earlier than our event |
382
|
|
|
* @param \DateTime $date |
383
|
|
|
* @return bool |
384
|
|
|
*/ |
385
|
|
View Code Duplication |
public function dateIsEarlier(\DateTime $date) { |
|
|
|
|
386
|
|
|
$dateEarlier = FALSE; |
387
|
|
|
|
388
|
|
|
$t1 = $this->start_date->getTimeStamp(); |
389
|
|
|
|
390
|
|
|
$t3 = $date->getTimeStamp(); |
391
|
|
|
|
392
|
|
|
if ($t3 < $t1) { |
393
|
|
|
$dateEarlier = TRUE; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
return $dateEarlier; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Checks if the date supplied ends after our event ends |
401
|
|
|
* @param \DateTime $date |
402
|
|
|
* @return bool |
403
|
|
|
*/ |
404
|
|
View Code Duplication |
public function dateIsLater(\DateTime $date) { |
|
|
|
|
405
|
|
|
$dateLater = FALSE; |
406
|
|
|
|
407
|
|
|
$t2 = $this->end_date->getTimeStamp(); |
408
|
|
|
|
409
|
|
|
$t4 = $date->getTimestamp(); |
410
|
|
|
|
411
|
|
|
if ($t2 < $t4) { |
412
|
|
|
$dateLater = TRUE; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
return $dateLater; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Checks if our event ends after the date supplied |
420
|
|
|
* @param \DateTime $date |
421
|
|
|
* @return bool |
422
|
|
|
*/ |
423
|
|
View Code Duplication |
public function endsLater(\DateTime $date) { |
|
|
|
|
424
|
|
|
$later = FALSE; |
425
|
|
|
|
426
|
|
|
$t2 = $this->end_date->getTimeStamp(); |
427
|
|
|
|
428
|
|
|
$t4 = $date->getTimestamp(); |
429
|
|
|
|
430
|
|
|
if ($t2 > $t4) { |
431
|
|
|
$later = TRUE; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
return $later; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Checks if our event starts earlier than the date supplied |
439
|
|
|
* @param \DateTime $date |
440
|
|
|
* @return bool |
441
|
|
|
*/ |
442
|
|
View Code Duplication |
public function startsEarlier(\DateTime $date) { |
|
|
|
|
443
|
|
|
$earlier = FALSE; |
444
|
|
|
|
445
|
|
|
$t1 = $this->start_date->getTimeStamp(); |
446
|
|
|
|
447
|
|
|
$t3 = $date->getTimestamp(); |
448
|
|
|
|
449
|
|
|
if ($t1 < $t3) { |
450
|
|
|
$earlier = TRUE; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
return $earlier; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Transforms the event in a breakdown of days, hours and minutes with associated states. |
458
|
|
|
* |
459
|
|
|
* @param EventItemizer $itemizer |
460
|
|
|
* @return array |
461
|
|
|
*/ |
462
|
|
|
public function itemize($itemizer) { |
463
|
|
|
$itemized = $itemizer->itemizeEvent(); |
464
|
|
|
return $itemized; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Saves an event using the Store object |
469
|
|
|
* |
470
|
|
|
* @param StoreInterface $store |
471
|
|
|
* @param string $granularity |
472
|
|
|
* |
473
|
|
|
* @return boolean |
474
|
|
|
*/ |
475
|
|
|
public function saveEvent(StoreInterface $store, $granularity = AbstractEvent::BAT_HOURLY) { |
476
|
|
|
return $store->storeEvent($this, $granularity); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* {@inheritdoc} |
481
|
|
|
*/ |
482
|
|
|
public function toJson(EventFormatter $event_formatter) { |
483
|
|
|
return $event_formatter->format($this); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
} |
487
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.