|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator\Components; |
|
4
|
|
|
|
|
5
|
|
|
use DateInterval; |
|
6
|
|
|
use DateTimeImmutable; |
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use Spatie\IcalendarGenerator\ComponentPayload; |
|
9
|
|
|
|
|
10
|
|
|
final class Event extends Component |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array */ |
|
13
|
|
|
private $alerts = []; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \DateTimeInterface */ |
|
16
|
|
|
private $starts; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \DateTimeInterface */ |
|
19
|
|
|
private $ends; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $name; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string|null */ |
|
25
|
|
|
private $description; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string|null */ |
|
28
|
|
|
private $location; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $uuid; |
|
32
|
|
|
|
|
33
|
|
|
/** @var \DateTimeInterface */ |
|
34
|
|
|
private $created; |
|
35
|
|
|
|
|
36
|
|
|
/** @var bool */ |
|
37
|
|
|
private $withTimezone = false; |
|
38
|
|
|
|
|
39
|
|
|
/** @var bool */ |
|
40
|
|
|
private $isFullDay = false; |
|
41
|
|
|
|
|
42
|
|
|
public static function create(string $name = null): Event |
|
43
|
|
|
{ |
|
44
|
|
|
return new self($name); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(string $name = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->name = $name; |
|
50
|
|
|
$this->uuid = uniqid(); |
|
51
|
|
|
$this->created = new DateTimeImmutable(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getComponentType(): string |
|
55
|
|
|
{ |
|
56
|
|
|
return 'EVENT'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getRequiredProperties(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
'UID', |
|
63
|
|
|
'DTSTAMP', |
|
64
|
|
|
'DTSTART', |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function startsAt(DateTimeInterface $starts): Event |
|
69
|
|
|
{ |
|
70
|
|
|
$this->starts = $starts; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function endsAt(DateTimeInterface $ends): Event |
|
76
|
|
|
{ |
|
77
|
|
|
$this->ends = $ends; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function period(DateTimeInterface $starts, DateTimeInterface $ends): Event |
|
83
|
|
|
{ |
|
84
|
|
|
$this->starts = $starts; |
|
85
|
|
|
$this->ends = $ends; |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function name(string $name): Event |
|
91
|
|
|
{ |
|
92
|
|
|
$this->name = $name; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function description(string $description): Event |
|
98
|
|
|
{ |
|
99
|
|
|
$this->description = $description; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function location(string $location): Event |
|
105
|
|
|
{ |
|
106
|
|
|
$this->location = $location; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function uniqueIdentifier(string $uid): Event |
|
112
|
|
|
{ |
|
113
|
|
|
$this->uuid = $uid; |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function createdAt(DateTimeInterface $created): Event |
|
119
|
|
|
{ |
|
120
|
|
|
$this->created = $created; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function withTimezone(): Event |
|
126
|
|
|
{ |
|
127
|
|
|
$this->withTimezone = true; |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function fullDay(): Event |
|
133
|
|
|
{ |
|
134
|
|
|
$this->isFullDay = true; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function alertMinutesBefore(int $minutes, string $message = null): Event |
|
140
|
|
|
{ |
|
141
|
|
|
$trigger = clone $this->starts; |
|
142
|
|
|
|
|
143
|
|
|
$this->alerts[] = Alert::create( |
|
144
|
|
|
$trigger->sub(new DateInterval("PT{$minutes}M")), |
|
145
|
|
|
$message ?? $this->name |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getPayload(): ComponentPayload |
|
152
|
|
|
{ |
|
153
|
|
|
return ComponentPayload::create($this->getComponentType()) |
|
154
|
|
|
->textProperty('UID', $this->uuid) |
|
155
|
|
|
->textProperty('SUMMARY', $this->name) |
|
156
|
|
|
->textProperty('DESCRIPTION', $this->description) |
|
157
|
|
|
->textProperty('LOCATION', $this->location) |
|
158
|
|
|
->dateTimeProperty('DTSTART', $this->starts, ! $this->isFullDay, $this->withTimezone) |
|
159
|
|
|
->dateTimeProperty('DTEND', $this->ends, ! $this->isFullDay, $this->withTimezone) |
|
160
|
|
|
->dateTimeProperty('DTSTAMP', $this->created, true, $this->withTimezone) |
|
161
|
|
|
->subComponent(...$this->alerts); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|