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 |
|
return $this->getEventsAfter(new \DateTimeImmutable()); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function getEventsAfter(\DateTimeInterface $date): array |
85
|
|
|
{ |
86
|
1 |
|
if (!$this->hasEvents()) { |
87
|
1 |
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$events = []; |
91
|
|
|
foreach ($this->getFlattenedEvents() as $event) { |
92
|
|
|
if ($event->getStartAt() > $date) { |
93
|
|
|
$events[] = $event; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $events; |
98
|
|
|
} |
99
|
|
|
|
100
|
7 |
|
public function hasEvents(): bool |
101
|
|
|
{ |
102
|
7 |
|
return 0 < count($this->events); |
103
|
|
|
} |
104
|
|
|
|
105
|
7 |
|
public function addEvent(Event $event): self |
106
|
|
|
{ |
107
|
7 |
|
$this->events[] = $event; |
108
|
|
|
|
109
|
7 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
8 |
|
public function getEvents(): array |
113
|
|
|
{ |
114
|
8 |
|
return $this->events; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.