4
4
5
5
use DateTime ;
6
6
use BaconQrCode \Exception \InvalidArgumentException ;
7
+ use DateTimeZone ;
7
8
use Exception ;
8
9
9
10
class Calendar implements DataTypeInterface
@@ -66,6 +67,14 @@ class Calendar implements DataTypeInterface
66
67
*/
67
68
protected $ endDateTime ;
68
69
70
+ /**
71
+ * Start/End date timezone.
72
+ * e.g 'Africa/Douala'
73
+ *
74
+ * @var string
75
+ */
76
+ protected $ timezone ;
77
+
69
78
/**
70
79
* The standard format for the [$statDateTime] and [$endDateTime].
71
80
* Gets overridden if user specifies their own formats [$dateTimeFormat].
@@ -81,6 +90,13 @@ class Calendar implements DataTypeInterface
81
90
*/
82
91
protected $ description ;
83
92
93
+ /**
94
+ * Alarm/Reminder. Trigger's before event
95
+ *
96
+ * @var string
97
+ */
98
+ protected $ alarm ;
99
+
84
100
/**
85
101
* Generates the DataType Object and sets all of its properties.
86
102
*
@@ -114,10 +130,18 @@ protected function buildCalendarString()
114
130
$ calendar .= "SUMMARY: " .$ this ->summary .$ this ->separator ;
115
131
}
116
132
if (isset ($ this ->startDateTime )) {
117
- $ calendar .= "DTSTART: " .$ this ->startDateTime .$ this ->separator ;
133
+ $ calendar .= "DTSTART " ;
134
+ if (isset ($ this ->timezone )) {
135
+ $ calendar .= $ this ->timezone ;
136
+ }
137
+ $ calendar .= ": " .$ this ->startDateTime .$ this ->separator ;
118
138
}
119
139
if (isset ($ this ->endDateTime )) {
120
- $ calendar .= "DTEND: " .$ this ->endDateTime .$ this ->separator ;
140
+ $ calendar .= "DTEND " ;
141
+ if (isset ($ this ->timezone )) {
142
+ $ calendar .= $ this ->timezone ;
143
+ }
144
+ $ calendar .= ": " .$ this ->endDateTime .$ this ->separator ;
121
145
}
122
146
if (isset ($ this ->location )) {
123
147
$ calendar .= "LOCATION: " .$ this ->location .$ this ->separator ;
@@ -128,6 +152,12 @@ protected function buildCalendarString()
128
152
if (isset ($ this ->description )) {
129
153
$ calendar .= "DESCRIPTION: " .$ this ->description .$ this ->separator ;
130
154
}
155
+ if (isset ($ this ->alarm )) {
156
+ $ calendar .= "BEGIN:VALARM " .$ this ->separator ;
157
+ $ calendar .= "TRIGGER:-PT " .$ this ->alarm .$ this ->separator ;
158
+ $ calendar .= "ACTION:DISPLAY " ;
159
+ $ calendar .= "END:VALARM " .$ this ->separator ;
160
+ }
131
161
132
162
$ calendar .= $ this ->suffix ;
133
163
@@ -151,6 +181,9 @@ protected function setProperties(array $arguments)
151
181
if (isset ($ arguments ['dateTimeFormat ' ])) {
152
182
$ this ->dateTimeFormat = $ arguments ['dateTimeFormat ' ];
153
183
}
184
+ if (isset ($ arguments ['timezone ' ])) {
185
+ $ this ->setEventTimeZone ($ arguments ['timezone ' ]);
186
+ }
154
187
155
188
$ this ->summary = $ arguments ['summary ' ];
156
189
$ this ->setEventDateTime ($ arguments ['startDateTime ' ], "start " );
@@ -261,4 +294,57 @@ protected function convertEventDateTimeToString($dateTime)
261
294
}
262
295
return $ date ->format ('yymd\THms ' );
263
296
}
297
+
298
+ /**
299
+ * Ensures alarm string is valid.
300
+ * e.g [0M, 20M, 10H, 100H]
301
+ * Runs before the event (10 minutes before [$startDateTime])
302
+ *
303
+ * @param string $alarm
304
+ *
305
+ * @return bool
306
+ */
307
+ protected function isValidAlarmTime ($ alarm )
308
+ {
309
+ return true ;
310
+ }
311
+
312
+ /**
313
+ * Ensures timezone string is valid. ('Africa/Douala')
314
+ *
315
+ * @param string $timezone
316
+ *
317
+ * @return bool
318
+ */
319
+ protected function isValidTimeZone ($ timezone )
320
+ {
321
+ if (! in_array ($ timezone , DateTimeZone::listIdentifiers ())) {
322
+ throw new InvalidArgumentException ('Invalid timezone provided. ' );
323
+ }
324
+ return true ;
325
+ }
326
+
327
+ /**
328
+ * Sets the timezone property.
329
+ *
330
+ * @param $timezone
331
+ */
332
+ protected function setEventTimeZone ($ timezone )
333
+ {
334
+ if ($ this ->isValidTimeZone ($ timezone )) {
335
+ $ this ->timezone = ";TZID= " .$ timezone ;
336
+ }
337
+ }
338
+
339
+ /**
340
+ * Sets the alarm property.
341
+ *
342
+ * @param $alarm
343
+ */
344
+ protected function setAlarm ($ alarm )
345
+ {
346
+ if ($ this ->isValidAlarmTime ($ alarm )) {
347
+ $ this ->alarm = $ alarm ;
348
+ }
349
+ }
264
350
}
0 commit comments