|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Shapin\Calendar\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Shapin\Calendar\EventsFlattener; |
|
8
|
|
|
|
|
9
|
|
|
class Calendar |
|
10
|
|
|
{ |
|
11
|
|
|
private $events = []; |
|
12
|
|
|
|
|
13
|
5 |
|
public function getFlattenedEvents(): array |
|
14
|
|
|
{ |
|
15
|
5 |
|
$flattener = new EventsFlattener(); |
|
16
|
|
|
|
|
17
|
5 |
|
$events = []; |
|
18
|
5 |
|
$eventsInARecurrence = []; |
|
19
|
5 |
|
$recurringEvents = []; |
|
20
|
5 |
|
foreach ($this->getEvents() as $event) { |
|
21
|
4 |
|
if ($event->isARecurrence()) { |
|
22
|
2 |
|
$eventsInARecurrence[] = $event; |
|
23
|
|
|
|
|
24
|
2 |
|
continue; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
4 |
|
if ($event->isRecurring()) { |
|
28
|
4 |
|
$recurringEvents[] = $event; |
|
29
|
|
|
|
|
30
|
4 |
|
continue; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$events[$event->getStartAt()->getTimestamp()] = $event; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Deal with recurring events |
|
37
|
5 |
|
foreach ($recurringEvents as $event) { |
|
38
|
4 |
|
$events += $flattener->flatten($event); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Replace recurring events with updated ones |
|
42
|
5 |
|
foreach ($eventsInARecurrence as $event) { |
|
43
|
2 |
|
$events[$event->getRecurrenceId()->getTimestamp()] = $event; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
ksort($events); |
|
47
|
|
|
|
|
48
|
5 |
|
return $events; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function hasEventBetween(\DateTimeImmutable $from, \DateTimeImmutable $to, bool $strict = true): bool |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
return 0 > count($this->getEventsBetween($form, $to, $strict)); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getEventsBetween(\DateTimeImmutable $from, \DateTimeImmutable $to, bool $strict = true) |
|
57
|
|
|
{ |
|
58
|
|
|
$events = []; |
|
59
|
|
|
foreach ($this->getFlattenedEvents() as $event) { |
|
60
|
|
|
if ($event->isBetween($from, $to, $strict)) { |
|
61
|
|
|
$events[] = $event; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $events; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function getNextEvent(): ?Event |
|
69
|
|
|
{ |
|
70
|
1 |
|
$nextEvents = $this->getUpcomingEvents(); |
|
71
|
|
|
|
|
72
|
1 |
|
if (0 === count($nextEvents)) { |
|
73
|
1 |
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return reset($nextEvents); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function getUpcomingEvents(): array |
|
80
|
|
|
{ |
|
81
|
1 |
|
if (!$this->hasEvents()) { |
|
82
|
1 |
|
return []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$now = new \DateTimeImmutable(); |
|
86
|
|
|
|
|
87
|
|
|
$events = []; |
|
88
|
|
|
foreach ($this->getFlattenedEvents() as $event) { |
|
89
|
|
|
if ($event->getStartAt() > $now) { |
|
90
|
|
|
$events[] = $event; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $events; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
7 |
|
public function hasEvents(): bool |
|
98
|
|
|
{ |
|
99
|
7 |
|
return 0 < count($this->events); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
7 |
|
public function addEvent(Event $event): self |
|
103
|
|
|
{ |
|
104
|
7 |
|
$this->events[] = $event; |
|
105
|
|
|
|
|
106
|
7 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
8 |
|
public function getEvents(): array |
|
110
|
|
|
{ |
|
111
|
8 |
|
return $this->events; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.