-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRecurringEvent.php
More file actions
115 lines (96 loc) · 3.14 KB
/
RecurringEvent.php
File metadata and controls
115 lines (96 loc) · 3.14 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
<?php
namespace TransformStudios\Events\Types;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use RRule\RRule;
use RRule\RRuleInterface;
use Spatie\IcalendarGenerator\Components\Event as ICalendarEvent;
use Spatie\IcalendarGenerator\Enums\RecurrenceFrequency;
use Spatie\IcalendarGenerator\ValueObjects\RRule as ICalendarRule;
class RecurringEvent extends Event
{
public function onSpecificDays(): array
{
return $this->specific_days ?? [];
}
public function interval(): int
{
return $this->interval ?? 1;
}
/**
* @return ICalendarEvent[]
*/
public function toICalendarEvents(): array
{
$iCalEvent = ICalendarEvent::create($this->event->title)
->uniqueIdentifier($this->event->id())
->startsAt($this->start())
->endsAt($this->end())
->rrule($this->spatieRule());
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];
}
protected function rule(): RRuleInterface
{
$rule = [
'dtstart' => $this->end(),
'freq' => $this->frequency(),
'interval' => $this->interval(),
];
if ($end = $this->end_date) {
$rule['until'] = Carbon::parse($end)->shiftTimezone($this->timezone['timezone'])->endOfDay();
}
if (! empty($days = $this->onSpecificDays())) {
$rule['byday'] = Arr::pluck($days, 'rrule');
}
return new RRule($rule);
}
private function frequency(): int
{
return match ($this->recurrence->value()) {
'daily' => Rrule::DAILY,
'weekly' => Rrule::WEEKLY,
'monthly' => Rrule::MONTHLY,
'yearly' => Rrule::YEARLY,
'every' => $this->periodToFrequency(),
default => Rrule::DAILY
};
}
private function frequencyToRecurrence(): RecurrenceFrequency
{
return match ($this->frequency()) {
Rrule::DAILY => RecurrenceFrequency::daily(),
Rrule::WEEKLY => RecurrenceFrequency::weekly(),
Rrule::MONTHLY => RecurrenceFrequency::monthly(),
Rrule::YEARLY => RecurrenceFrequency::yearly(),
default => RecurrenceFrequency::daily()
};
}
private function periodToFrequency(): int
{
return match ($this->period->value()) {
'days' => Rrule::DAILY,
'weeks' => Rrule::WEEKLY,
'months' => Rrule::MONTHLY,
'years' => Rrule::YEARLY,
default => Rrule::DAILY
};
}
private function spatieRule(): ICalendarRule
{
$rule = ICalendarRule::frequency($this->frequencyToRecurrence())
->interval($this->interval());
if ($end = $this->end_date) {
$rule->until(Carbon::parse($end)->endOfDay());
}
return $rule;
}
}