1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\IcalendarGenerator\Components; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use DateTimeInterface; |
8
|
|
|
use Spatie\IcalendarGenerator\ComponentPayload; |
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 string */ |
54
|
|
|
private $recurrenceRule; |
55
|
|
|
|
56
|
|
|
public static function create(string $name = null): Event |
57
|
|
|
{ |
58
|
|
|
return new self($name); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function __construct(string $name = null) |
62
|
|
|
{ |
63
|
|
|
$this->name = $name; |
64
|
|
|
$this->uuid = uniqid(); |
65
|
|
|
$this->created = new DateTimeImmutable(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getComponentType(): string |
69
|
|
|
{ |
70
|
|
|
return 'EVENT'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getRequiredProperties(): array |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
'UID', |
77
|
|
|
'DTSTAMP', |
78
|
|
|
'DTSTART', |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function startsAt(DateTimeInterface $starts): Event |
83
|
|
|
{ |
84
|
|
|
$this->starts = $starts; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function endsAt(DateTimeInterface $ends): Event |
90
|
|
|
{ |
91
|
|
|
$this->ends = $ends; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function period(DateTimeInterface $starts, DateTimeInterface $ends): Event |
97
|
|
|
{ |
98
|
|
|
$this->starts = $starts; |
99
|
|
|
$this->ends = $ends; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function name(string $name): Event |
105
|
|
|
{ |
106
|
|
|
$this->name = $name; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function description(string $description): Event |
112
|
|
|
{ |
113
|
|
|
$this->description = $description; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function address(string $address, string $name = null): Event |
119
|
|
|
{ |
120
|
|
|
$this->address = $address; |
121
|
|
|
|
122
|
|
|
if ($name) { |
|
|
|
|
123
|
|
|
$this->addressName = $name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function addressName(string $name): Event |
130
|
|
|
{ |
131
|
|
|
$this->addressName = $name; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function coordinates(float $lat, float $lng): Event |
137
|
|
|
{ |
138
|
|
|
$this->lat = $lat; |
139
|
|
|
$this->lng = $lng; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function uniqueIdentifier(string $uid): Event |
145
|
|
|
{ |
146
|
|
|
$this->uuid = $uid; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function createdAt(DateTimeInterface $created): Event |
152
|
|
|
{ |
153
|
|
|
$this->created = $created; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function withTimezone(): Event |
159
|
|
|
{ |
160
|
|
|
$this->withTimezone = true; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function fullDay(): Event |
166
|
|
|
{ |
167
|
|
|
$this->isFullDay = true; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function alertMinutesBefore(int $minutes, string $message = null): Event |
173
|
|
|
{ |
174
|
|
|
$trigger = clone $this->starts; |
175
|
|
|
|
176
|
|
|
$this->alerts[] = Alert::create( |
177
|
|
|
$trigger->sub(new DateInterval("PT{$minutes}M")), |
178
|
|
|
$message ?? $this->name |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function recurrenceRule(string $rule): Event |
185
|
|
|
{ |
186
|
|
|
$this->recurrenceRule = $rule; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getPayload(): ComponentPayload |
192
|
|
|
{ |
193
|
|
|
$payload = ComponentPayload::create($this->getComponentType()) |
194
|
|
|
->textProperty('UID', $this->uuid) |
195
|
|
|
->textProperty('SUMMARY', $this->name) |
196
|
|
|
->textProperty('DESCRIPTION', $this->description) |
197
|
|
|
->textProperty('LOCATION', $this->address) |
198
|
|
|
->dateTimeProperty('DTSTART', $this->starts, ! $this->isFullDay, $this->withTimezone) |
199
|
|
|
->dateTimeProperty('DTEND', $this->ends, ! $this->isFullDay, $this->withTimezone) |
200
|
|
|
->dateTimeProperty('DTSTAMP', $this->created, true, $this->withTimezone) |
201
|
|
|
->textProperty('RRULE', $this->recurrenceRule) |
202
|
|
|
->subComponent(...$this->alerts); |
203
|
|
|
|
204
|
|
|
$payload = $this->resolveLocationProperties($payload); |
205
|
|
|
|
206
|
|
|
return $payload; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
private function resolveLocationProperties(ComponentPayload $payload): ComponentPayload |
210
|
|
|
{ |
211
|
|
|
if (is_null($this->lng) && is_null($this->lat)) { |
212
|
|
|
return $payload; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$payload->property(CoordinatesPropertyType::create('GEO', $this->lat, $this->lng)); |
216
|
|
|
|
217
|
|
|
if (is_null($this->address)) { |
218
|
|
|
return $payload; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$property = CoordinatesPropertyType::create( |
222
|
|
|
'X-APPLE-STRUCTURED-LOCATION', $this->lat, $this->lng |
223
|
|
|
)->addParameter(Parameter::create('VALUE', 'URI')) |
224
|
|
|
->addParameter(Parameter::create('X-ADDRESS', $this->address)) |
225
|
|
|
->addParameter(Parameter::create('X-APPLE-RADIUS', 72)) |
226
|
|
|
->addParameter(Parameter::create('X-TITLE', $this->addressName)); |
227
|
|
|
|
228
|
|
|
$payload->property($property); |
229
|
|
|
|
230
|
|
|
return $payload; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
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: