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