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