-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMultiDayEvent.php
More file actions
147 lines (125 loc) · 4.39 KB
/
MultiDayEvent.php
File metadata and controls
147 lines (125 loc) · 4.39 KB
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
<?php
namespace TransformStudios\Events\Types;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use RRule\RRule;
use RRule\RRuleInterface;
use RRule\RSet;
use Spatie\IcalendarGenerator\Components\Event as ICalendarEvent;
use Statamic\Entries\Entry;
use Statamic\Fields\Values;
use TransformStudios\Events\Day;
class MultiDayEvent extends Event
{
private Collection $days;
public function __construct(Entry $event, private bool $collapseMultiDays)
{
parent::__construct($event);
$this->days = collect($this->event->days)
->sortBy('date')
->map(fn (Values $day) => new Day(
$day->all(),
$this->timezone['timezone'],
$day->all_day || $this->isAllDay(),
));
}
/**
* @template T of TransformStudios\Events\Day
*
* @return Collection<T>
*/
public function days(): Collection
{
return $this->days;
}
public function end(): CarbonImmutable
{
return $this->days->last()->end();
}
public function start(): CarbonImmutable
{
return $this->days->first()->start();
}
public function toICalendarEvent(string|CarbonInterface $date): ?ICalendarEvent
{
if (! $this->occursOnDate($date)) {
return null;
}
$immutableDate = $this->toCarbonImmutable($date);
$day = $this->getDayFromDate($immutableDate);
$iCalEvent = ICalendarEvent::create($this->event->title)
->uniqueIdentifier($this->event->id())
->startsAt($immutableDate->setTimeFromTimeString($day->start()))
->endsAt($immutableDate->setTimeFromTimeString($day->end()));
if (! is_null($location = $this->location($this->event))) {
$iCalEvent->address($location);
}
if (! is_null($description = $this->event->description)) {
$iCalEvent->description($description);
}
if (! is_null($link = $this->event->link)) {
$iCalEvent->url($link);
}
return $iCalEvent;
}
/**
* @return ICalendarEvent[]
*/
public function toICalendarEvents(): array
{
return collect($this->days)
->map(fn (Day $day, int $index) => $day->toICalendarEvent($this->event->title, $index))
->all();
}
protected function rule(bool $collapseDays = false): RRuleInterface
{
// if we're collapsing, then return an rrule instead of rset and use start of first day to end of last day
if ($this->collapseMultiDays) {
return new RRule([
'count' => 1,
'dtstart' => $this->end(),
'freq' => RRule::DAILY,
]);
}
return tap(
new RSet,
fn (RSet $rset) => $this->days->each(fn (Day $day) => $rset->addRRule([
'count' => 1,
'dtstart' => $day->end()->subSecond(),
'freq' => RRule::SECONDLY,
]))
);
}
protected function supplement(CarbonInterface $date): ?Entry
{
if (! $day = $this->getDayFromDate($date)) {
return null;
}
if ($this->collapseMultiDays) {
return tap(
unserialize(serialize($this->event)),
fn (Entry $occurrence) => $occurrence
->setSupplement('multi_day', true)
->setSupplement('collapse_multi_days', true)
->setSupplement('start', $this->start())
->setSupplement('end', $this->end())
->setSupplement('has_end_time', $this->hasEndTime())
);
}
return tap(
unserialize(serialize($this->event)),
fn (Entry $occurrence) => $occurrence
->setSupplement('all_day', $day->isAllDay())
->setSupplement('multi_day', true)
->setSupplement('collapse_multi_days', $this->collapseMultiDays)
->setSupplement('start', $day->start())
->setSupplement('end', $day->end())
->setSupplement('has_end_time', $day->hasEndTime())
);
}
private function getDayFromDate(CarbonInterface $date): ?Day
{
return $this->days->first(fn (Day $day, int $index) => $this->collapseMultiDays ? $index == 0 : $date->isSameDay($day->start()));
}
}