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