Skip to content

Commit 01c5495

Browse files
committed
added timezone, and description for event's notes
Signed-off-by: Mua N. Laurent <[email protected]>
1 parent d2c32f8 commit 01c5495

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

src/DataTypes/Calendar.php

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use BaconQrCode\Exception\InvalidArgumentException;
7+
use DateTimeZone;
78
use Exception;
89

910
class Calendar implements DataTypeInterface
@@ -66,6 +67,14 @@ class Calendar implements DataTypeInterface
6667
*/
6768
protected $endDateTime;
6869

70+
/**
71+
* Start/End date timezone.
72+
* e.g 'Africa/Douala'
73+
*
74+
* @var string
75+
*/
76+
protected $timezone;
77+
6978
/**
7079
* The standard format for the [$statDateTime] and [$endDateTime].
7180
* Gets overridden if user specifies their own formats [$dateTimeFormat].
@@ -81,6 +90,13 @@ class Calendar implements DataTypeInterface
8190
*/
8291
protected $description;
8392

93+
/**
94+
* Alarm/Reminder. Trigger's before event
95+
*
96+
* @var string
97+
*/
98+
protected $alarm;
99+
84100
/**
85101
* Generates the DataType Object and sets all of its properties.
86102
*
@@ -114,10 +130,18 @@ protected function buildCalendarString()
114130
$calendar .= "SUMMARY:".$this->summary.$this->separator;
115131
}
116132
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;
118138
}
119139
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;
121145
}
122146
if (isset($this->location)) {
123147
$calendar .= "LOCATION:".$this->location.$this->separator;
@@ -128,6 +152,12 @@ protected function buildCalendarString()
128152
if (isset($this->description)) {
129153
$calendar .= "DESCRIPTION:".$this->description.$this->separator;
130154
}
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+
}
131161

132162
$calendar .= $this->suffix;
133163

@@ -151,6 +181,9 @@ protected function setProperties(array $arguments)
151181
if (isset($arguments['dateTimeFormat'])) {
152182
$this->dateTimeFormat = $arguments['dateTimeFormat'];
153183
}
184+
if (isset($arguments['timezone'])) {
185+
$this->setEventTimeZone($arguments['timezone']);
186+
}
154187

155188
$this->summary = $arguments['summary'];
156189
$this->setEventDateTime($arguments['startDateTime'], "start");
@@ -261,4 +294,57 @@ protected function convertEventDateTimeToString($dateTime)
261294
}
262295
return $date->format('yymd\THms');
263296
}
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+
}
264350
}

tests/DataTypes/CalendarTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public function test_it_generates_a_proper_format_with_calendar_parameters()
173173
'startDateTime' => '2020-10-08 16:00',
174174
'endDateTime' => '2020-10-08 18:00',
175175
'location' => 'Foo Location',
176+
'timezone' => 'Africa/Douala',
176177
'url' => 'https://www.google.com',
177178
'dateTimeFormat' => 'Y-m-d H:i',
178179
'description' => 'FooBar\'s Event description can be longer than this.'
@@ -181,8 +182,8 @@ public function test_it_generates_a_proper_format_with_calendar_parameters()
181182

182183
$properFormat = "BEGIN:VEVENT".$this->separator.
183184
"SUMMARY:My FooBar Event".$this->separator.
184-
"DTSTART:20201008T161000".$this->separator.
185-
"DTEND:20201008T181000".$this->separator.
185+
"DTSTART;TZID=Africa/Douala:20201008T161000".$this->separator.
186+
"DTEND;TZID=Africa/Douala:20201008T181000".$this->separator.
186187
"LOCATION:Foo Location".$this->separator.
187188
"URL:https://www.google.com".$this->separator.
188189
"DESCRIPTION:FooBar's Event description can be longer than this.".$this->separator.

0 commit comments

Comments
 (0)