1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator\Components; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use Spatie\IcalendarGenerator\ComponentPayload; |
8
|
|
|
use Spatie\IcalendarGenerator\Enums\Classification; |
9
|
|
|
use Spatie\IcalendarGenerator\Enums\ParticipationStatus; |
10
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\CalendarAddressPropertyType; |
11
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\CoordinatesPropertyType; |
12
|
|
|
use Spatie\IcalendarGenerator\PropertyTypes\Parameter; |
13
|
|
|
use Spatie\IcalendarGenerator\ValueObjects\CalendarAddress; |
14
|
|
|
|
15
|
|
|
final class Event extends Component |
16
|
|
|
{ |
17
|
|
|
/** @var array */ |
18
|
|
|
private $alerts = []; |
19
|
|
|
|
20
|
|
|
/** @var \DateTimeInterface */ |
21
|
|
|
private $starts; |
22
|
|
|
|
23
|
|
|
/** @var \DateTimeInterface */ |
24
|
|
|
private $ends; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $name; |
28
|
|
|
|
29
|
|
|
/** @var string|null */ |
30
|
|
|
private $description; |
31
|
|
|
|
32
|
|
|
/** @var string|null */ |
33
|
|
|
private $address; |
34
|
|
|
|
35
|
|
|
/** @var string|null */ |
36
|
|
|
private $addressName; |
37
|
|
|
|
38
|
|
|
/** @var float|null */ |
39
|
|
|
private $lat; |
40
|
|
|
|
41
|
|
|
/** @var float|null */ |
42
|
|
|
private $lng; |
43
|
|
|
|
44
|
|
|
/** @var string */ |
45
|
|
|
private $uuid; |
46
|
|
|
|
47
|
|
|
/** @var \DateTimeInterface */ |
48
|
|
|
private $created; |
49
|
|
|
|
50
|
|
|
/** @var bool */ |
51
|
|
|
private $withTimezone = false; |
52
|
|
|
|
53
|
|
|
/** @var bool */ |
54
|
|
|
private $isFullDay = false; |
55
|
|
|
|
56
|
|
|
/** @var \Spatie\IcalendarGenerator\Enums\Classification|null */ |
57
|
|
|
private $classification = null; |
58
|
|
|
|
59
|
|
|
/** @var bool|null */ |
60
|
|
|
private $transparent = null; |
61
|
|
|
|
62
|
|
|
/** @var \Spatie\IcalendarGenerator\ValueObjects\CalendarAddress[] */ |
63
|
|
|
private $attendees = []; |
64
|
|
|
|
65
|
|
|
/** @var \Spatie\IcalendarGenerator\ValueObjects\CalendarAddress|null */ |
66
|
|
|
private $organizer = null; |
67
|
|
|
|
68
|
|
|
public static function create(string $name = null): Event |
69
|
|
|
{ |
70
|
|
|
return new self($name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __construct(string $name = null) |
74
|
|
|
{ |
75
|
|
|
$this->name = $name; |
76
|
|
|
$this->uuid = uniqid(); |
77
|
|
|
$this->created = new DateTimeImmutable(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getComponentType(): string |
81
|
|
|
{ |
82
|
|
|
return 'EVENT'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getRequiredProperties(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'UID', |
89
|
|
|
'DTSTAMP', |
90
|
|
|
'DTSTART', |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function startsAt(DateTimeInterface $starts): Event |
95
|
|
|
{ |
96
|
|
|
$this->starts = $starts; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function endsAt(DateTimeInterface $ends): Event |
102
|
|
|
{ |
103
|
|
|
$this->ends = $ends; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function period(DateTimeInterface $starts, DateTimeInterface $ends): Event |
109
|
|
|
{ |
110
|
|
|
$this->starts = $starts; |
111
|
|
|
$this->ends = $ends; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function name(string $name): Event |
117
|
|
|
{ |
118
|
|
|
$this->name = $name; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function description(string $description): Event |
124
|
|
|
{ |
125
|
|
|
$this->description = $description; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function address(string $address, string $name = null): Event |
131
|
|
|
{ |
132
|
|
|
$this->address = $address; |
133
|
|
|
|
134
|
|
|
if ($name) { |
|
|
|
|
135
|
|
|
$this->addressName = $name; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function addressName(string $name): Event |
142
|
|
|
{ |
143
|
|
|
$this->addressName = $name; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function coordinates(float $lat, float $lng): Event |
149
|
|
|
{ |
150
|
|
|
$this->lat = $lat; |
151
|
|
|
$this->lng = $lng; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function uniqueIdentifier(string $uid): Event |
157
|
|
|
{ |
158
|
|
|
$this->uuid = $uid; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function createdAt(DateTimeInterface $created): Event |
164
|
|
|
{ |
165
|
|
|
$this->created = $created; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function withTimezone(): Event |
171
|
|
|
{ |
172
|
|
|
$this->withTimezone = true; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function fullDay(): Event |
178
|
|
|
{ |
179
|
|
|
$this->isFullDay = true; |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function alert(Alert $alert): Event |
185
|
|
|
{ |
186
|
|
|
$this->alerts[] = $alert; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function alertMinutesBefore(int $minutes, string $message = null): Event |
192
|
|
|
{ |
193
|
|
|
$this->alerts[] = Alert::minutesBeforeStart($minutes, $message); |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function alertMinutesAfter(int $minutes, string $message = null): Event |
199
|
|
|
{ |
200
|
|
|
$this->alerts[] = Alert::minutesAfterEnd($minutes, $message); |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function classification(?Classification $classification): Event |
206
|
|
|
{ |
207
|
|
|
$this->classification = $classification; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function transparent(): Event |
213
|
|
|
{ |
214
|
|
|
$this->transparent = true; |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function attendee( |
220
|
|
|
string $email, |
221
|
|
|
string $name = null, |
222
|
|
|
ParticipationStatus $participationStatus = null |
223
|
|
|
): Event { |
224
|
|
|
$this->attendees[] = new CalendarAddress($email, $name, $participationStatus); |
225
|
|
|
|
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function organizer(string $email, string $name = null): Event |
230
|
|
|
{ |
231
|
|
|
$this->organizer = new CalendarAddress($email, $name); |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
protected function payload(): ComponentPayload |
237
|
|
|
{ |
238
|
|
|
$payload = ComponentPayload::create($this->getComponentType()) |
239
|
|
|
->textProperty('UID', $this->uuid) |
240
|
|
|
->textProperty('SUMMARY', $this->name) |
241
|
|
|
->textProperty('DESCRIPTION', $this->description) |
242
|
|
|
->textProperty('LOCATION', $this->address) |
243
|
|
|
->textProperty('CLASS', $this->classification) |
|
|
|
|
244
|
|
|
->textProperty('TRANSP', $this->transparent ? 'TRANSPARENT' : null) |
245
|
|
|
->dateTimeProperty('DTSTART', $this->starts, ! $this->isFullDay, $this->withTimezone) |
246
|
|
|
->dateTimeProperty('DTEND', $this->ends, ! $this->isFullDay, $this->withTimezone) |
247
|
|
|
->dateTimeProperty('DTSTAMP', $this->created, true, $this->withTimezone) |
248
|
|
|
->subComponent(...$this->alerts); |
249
|
|
|
|
250
|
|
|
if ($this->organizer) { |
251
|
|
|
$payload->property(CalendarAddressPropertyType::create('ORGANIZER', $this->organizer)); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
foreach ($this->attendees as $attendee) { |
255
|
|
|
$payload->property(CalendarAddressPropertyType::create('ATTENDEE', $attendee)); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$payload = $this->resolveLocationProperties($payload); |
259
|
|
|
|
260
|
|
|
return $payload; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
private function resolveLocationProperties(ComponentPayload $payload): ComponentPayload |
264
|
|
|
{ |
265
|
|
|
if (is_null($this->lng) && is_null($this->lat)) { |
266
|
|
|
return $payload; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$payload->property(CoordinatesPropertyType::create('GEO', $this->lat, $this->lng)); |
270
|
|
|
|
271
|
|
|
if (is_null($this->address)) { |
272
|
|
|
return $payload; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$property = CoordinatesPropertyType::create( |
276
|
|
|
'X-APPLE-STRUCTURED-LOCATION', |
277
|
|
|
$this->lat, |
278
|
|
|
$this->lng |
279
|
|
|
)->addParameter(Parameter::create('VALUE', 'URI')) |
280
|
|
|
->addParameter(Parameter::create('X-ADDRESS', $this->address)) |
281
|
|
|
->addParameter(Parameter::create('X-APPLE-RADIUS', 72)) |
282
|
|
|
->addParameter(Parameter::create('X-TITLE', $this->addressName)); |
283
|
|
|
|
284
|
|
|
$payload->property($property); |
285
|
|
|
|
286
|
|
|
return $payload; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: