4
4
5
5
use DateTime ;
6
6
use BaconQrCode \Exception \InvalidArgumentException ;
7
+ use Exception ;
7
8
8
9
class Calendar implements DataTypeInterface
9
10
{
@@ -12,7 +13,7 @@ class Calendar implements DataTypeInterface
12
13
*
13
14
* @var string
14
15
*/
15
- protected $ prefix = "BEGIN:VEVENT \n " ;
16
+ protected $ prefix = "BEGIN:VEVENT " ;
16
17
17
18
/**
18
19
* The suffix of the QrCode.
@@ -26,7 +27,7 @@ class Calendar implements DataTypeInterface
26
27
*
27
28
* @var string
28
29
*/
29
- protected $ separator = "\n" ;
30
+ protected $ separator = "\r\ n" ;
30
31
31
32
/**
32
33
* The summary of the event.
@@ -51,20 +52,23 @@ class Calendar implements DataTypeInterface
51
52
52
53
/**
53
54
* The start time of the event.
55
+ * e.g 2020-05-23 15:00
54
56
*
55
- * @var DateTime
57
+ * @var string
56
58
*/
57
59
protected $ startDateTime ;
58
60
59
61
/**
60
62
* The end time of the event.
63
+ * e.g 2020-05-24 15:00
61
64
*
62
- * @var DateTime
65
+ * @var string
63
66
*/
64
67
protected $ endDateTime ;
65
68
66
69
/**
67
70
* The standard format for the [$statDateTime] and [$endDateTime].
71
+ * Gets overridden if user specifies their own formats [$dateTimeFormat].
68
72
*
69
73
* @var string
70
74
*/
@@ -97,7 +101,7 @@ public function __toString()
97
101
*/
98
102
protected function buildCalendarString ()
99
103
{
100
- $ calendar = $ this ->prefix ;
104
+ $ calendar = $ this ->prefix . $ this -> separator ;
101
105
102
106
if (isset ($ this ->summary )) {
103
107
$ calendar .= "SUMMARY: " .$ this ->summary .$ this ->separator ;
@@ -109,10 +113,10 @@ protected function buildCalendarString()
109
113
$ calendar .= "URL: " .$ this ->url .$ this ->separator ;
110
114
}
111
115
if (isset ($ this ->startDateTime )) {
112
- $ calendar .= "DTSTART: " .$ this ->convertEventDateTimeToString ( $ this -> startDateTime ) .$ this ->separator ;
116
+ $ calendar .= "DTSTART: " .$ this ->startDateTime .$ this ->separator ;
113
117
}
114
118
if (isset ($ this ->endDateTime )) {
115
- $ calendar .= "DTEND: " .$ this ->convertEventDateTimeToString ( $ this -> endDateTime ) .$ this ->separator ;
119
+ $ calendar .= "DTEND: " .$ this ->endDateTime .$ this ->separator ;
116
120
}
117
121
118
122
$ calendar .= $ this ->suffix ;
@@ -128,20 +132,28 @@ protected function buildCalendarString()
128
132
protected function setProperties (array $ arguments )
129
133
{
130
134
$ arguments = $ arguments [0 ];
131
- if (isset ($ arguments ['summary ' ])) {
135
+ if (! isset ($ arguments ['summary ' ])) {
136
+ throw new InvalidArgumentException ('Please provide an event summary. ' );
137
+ } elseif (! isset ($ arguments ['startDateTime ' ])) {
138
+ throw new InvalidArgumentException ('Please provide a start date for the event. ' );
139
+ } else {
140
+
141
+ if (isset ($ arguments ['dateTimeFormat ' ])) {
142
+ $ this ->dateTimeFormat = $ arguments ['dateTimeFormat ' ];
143
+ }
144
+
132
145
$ this ->summary = $ arguments ['summary ' ];
133
- }
134
- if (isset ($ arguments ['location ' ])) {
135
- $ this ->location = $ arguments ['location ' ];
136
- }
137
- if (isset ($ arguments ['url ' ])) {
138
- $ this ->setUrl ($ arguments ['url ' ]);
139
- }
140
- if (isset ($ arguments ['start_date_time ' ])) {
141
- $ this ->setEventDateTime ($ arguments ['start_date_time ' ], "start " );
142
- }
143
- if (isset ($ arguments ['end_date_time ' ])) {
144
- $ this ->setEventDateTime ($ arguments ['end_date_time ' ], "end " );
146
+ $ this ->setEventDateTime ($ arguments ['startDateTime ' ], "start " );
147
+
148
+ if (isset ($ arguments ['location ' ])) {
149
+ $ this ->location = $ arguments ['location ' ];
150
+ }
151
+ if (isset ($ arguments ['url ' ])) {
152
+ $ this ->setUrl ($ arguments ['url ' ]);
153
+ }
154
+ if (isset ($ arguments ['endDateTime ' ])) {
155
+ $ this ->setEventDateTime ($ arguments ['endDateTime ' ], "end " );
156
+ }
145
157
}
146
158
}
147
159
@@ -160,18 +172,18 @@ protected function setUrl($url)
160
172
/**
161
173
* Sets the datetime property.
162
174
*
163
- * @param DateTime $eventDateTime
175
+ * @param string $eventDateTime
164
176
* @param string $type
165
177
*
166
178
*/
167
179
protected function setEventDateTime ($ eventDateTime , $ type = "" )
168
180
{
169
- if ($ this ->isValidDateTime ($ eventDateTime )) {
181
+ if ($ this ->isValidDateTime ($ eventDateTime, $ type )) {
170
182
if ($ type == "start " ) {
171
- $ this ->startDateTime = $ eventDateTime -> format ( " yyyyMMddTHHmmss " );
183
+ $ this ->startDateTime = $ this -> convertEventDateTimeToString ( $ eventDateTime );
172
184
}
173
185
if ($ type == "end " ) {
174
- $ this ->endDateTime = $ eventDateTime -> format ( " yyyyMMddTHHmmss " );
186
+ $ this ->endDateTime = $ this -> convertEventDateTimeToString ( $ eventDateTime );
175
187
}
176
188
}
177
189
}
@@ -185,7 +197,7 @@ protected function setEventDateTime($eventDateTime, $type = "")
185
197
*/
186
198
protected function isValidUrl ($ url )
187
199
{
188
- if (! filter_var ($ url , FILTER_VALIDATE_EMAIL )) {
200
+ if (! filter_var ($ url , FILTER_VALIDATE_URL )) {
189
201
throw new InvalidArgumentException ('Invalid url provided ' );
190
202
}
191
203
@@ -195,36 +207,45 @@ protected function isValidUrl($url)
195
207
/**
196
208
* Ensures datetime is valid.
197
209
*
198
- * @param DateTime $dateTime
210
+ * @param string $dateTime
199
211
* @param string $type
200
212
*
201
213
* @return bool
202
214
*/
203
215
protected function isValidDateTime ($ dateTime , $ type = "" )
204
216
{
205
217
$ newDate = DateTime::createFromFormat ($ this ->dateTimeFormat , $ dateTime );
206
- if (! $ newDate && $ newDate ->format ($ this ->dateTimeFormat ) == $ dateTime ) {
218
+ if (! ( $ newDate && $ newDate ->format ($ this ->dateTimeFormat ) == $ dateTime) ) {
207
219
if ($ type == "start " ) {
208
- throw new InvalidArgumentException ('Invalid start date provided ' );
220
+ throw new InvalidArgumentException ('Invalid start date provided. Date must be of format ' .
221
+ $ this ->dateTimeFormat );
209
222
}
210
223
if ($ type == "end " ) {
211
- throw new InvalidArgumentException ('Invalid end date provided ' );
224
+ throw new InvalidArgumentException ('Invalid end date provided. Date must be of format ' .
225
+ $ this ->dateTimeFormat );
212
226
}
213
- throw new InvalidArgumentException ('Invalid date provided ' );
227
+ throw new InvalidArgumentException ('Invalid date provided. Date must be of format ' .
228
+ $ this ->dateTimeFormat );
214
229
}
215
230
216
231
return true ;
217
232
}
218
233
234
+
219
235
/**
220
- * Returns date time to string
236
+ * Returns correct date time to string
221
237
*
222
- * @param DateTime $dateTime
238
+ * @param string $dateTime
223
239
*
224
240
* @return string
225
241
*/
226
242
protected function convertEventDateTimeToString ($ dateTime )
227
243
{
228
- return $ dateTime ->format ($ this ->dateTimeFormat );
244
+ try {
245
+ $ date = new DateTime ($ dateTime );
246
+ } catch (Exception $ e ) {
247
+ throw new InvalidArgumentException ('Invalid date provided ' );
248
+ }
249
+ return $ date ->format ('yymdTHms ' );
229
250
}
230
251
}
0 commit comments