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