44
55use DateTime ;
66use BaconQrCode \Exception \InvalidArgumentException ;
7+ use Exception ;
78
89class Calendar implements DataTypeInterface
910{
@@ -12,7 +13,7 @@ class Calendar implements DataTypeInterface
1213 *
1314 * @var string
1415 */
15- protected $ prefix = "BEGIN:VEVENT \n " ;
16+ protected $ prefix = "BEGIN:VEVENT " ;
1617
1718 /**
1819 * The suffix of the QrCode.
@@ -26,7 +27,7 @@ class Calendar implements DataTypeInterface
2627 *
2728 * @var string
2829 */
29- protected $ separator = "\n" ;
30+ protected $ separator = "\r\ n" ;
3031
3132 /**
3233 * The summary of the event.
@@ -51,20 +52,23 @@ class Calendar implements DataTypeInterface
5152
5253 /**
5354 * The start time of the event.
55+ * e.g 2020-05-23 15:00
5456 *
55- * @var DateTime
57+ * @var string
5658 */
5759 protected $ startDateTime ;
5860
5961 /**
6062 * The end time of the event.
63+ * e.g 2020-05-24 15:00
6164 *
62- * @var DateTime
65+ * @var string
6366 */
6467 protected $ endDateTime ;
6568
6669 /**
6770 * The standard format for the [$statDateTime] and [$endDateTime].
71+ * Gets overridden if user specifies their own formats [$dateTimeFormat].
6872 *
6973 * @var string
7074 */
@@ -97,7 +101,7 @@ public function __toString()
97101 */
98102 protected function buildCalendarString ()
99103 {
100- $ calendar = $ this ->prefix ;
104+ $ calendar = $ this ->prefix . $ this -> separator ;
101105
102106 if (isset ($ this ->summary )) {
103107 $ calendar .= "SUMMARY: " .$ this ->summary .$ this ->separator ;
@@ -109,10 +113,10 @@ protected function buildCalendarString()
109113 $ calendar .= "URL: " .$ this ->url .$ this ->separator ;
110114 }
111115 if (isset ($ this ->startDateTime )) {
112- $ calendar .= "DTSTART: " .$ this ->convertEventDateTimeToString ( $ this -> startDateTime ) .$ this ->separator ;
116+ $ calendar .= "DTSTART: " .$ this ->startDateTime .$ this ->separator ;
113117 }
114118 if (isset ($ this ->endDateTime )) {
115- $ calendar .= "DTEND: " .$ this ->convertEventDateTimeToString ( $ this -> endDateTime ) .$ this ->separator ;
119+ $ calendar .= "DTEND: " .$ this ->endDateTime .$ this ->separator ;
116120 }
117121
118122 $ calendar .= $ this ->suffix ;
@@ -128,20 +132,28 @@ protected function buildCalendarString()
128132 protected function setProperties (array $ arguments )
129133 {
130134 $ 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+
132145 $ 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+ }
145157 }
146158 }
147159
@@ -160,18 +172,18 @@ protected function setUrl($url)
160172 /**
161173 * Sets the datetime property.
162174 *
163- * @param DateTime $eventDateTime
175+ * @param string $eventDateTime
164176 * @param string $type
165177 *
166178 */
167179 protected function setEventDateTime ($ eventDateTime , $ type = "" )
168180 {
169- if ($ this ->isValidDateTime ($ eventDateTime )) {
181+ if ($ this ->isValidDateTime ($ eventDateTime, $ type )) {
170182 if ($ type == "start " ) {
171- $ this ->startDateTime = $ eventDateTime -> format ( " yyyyMMddTHHmmss " );
183+ $ this ->startDateTime = $ this -> convertEventDateTimeToString ( $ eventDateTime );
172184 }
173185 if ($ type == "end " ) {
174- $ this ->endDateTime = $ eventDateTime -> format ( " yyyyMMddTHHmmss " );
186+ $ this ->endDateTime = $ this -> convertEventDateTimeToString ( $ eventDateTime );
175187 }
176188 }
177189 }
@@ -185,7 +197,7 @@ protected function setEventDateTime($eventDateTime, $type = "")
185197 */
186198 protected function isValidUrl ($ url )
187199 {
188- if (! filter_var ($ url , FILTER_VALIDATE_EMAIL )) {
200+ if (! filter_var ($ url , FILTER_VALIDATE_URL )) {
189201 throw new InvalidArgumentException ('Invalid url provided ' );
190202 }
191203
@@ -195,36 +207,45 @@ protected function isValidUrl($url)
195207 /**
196208 * Ensures datetime is valid.
197209 *
198- * @param DateTime $dateTime
210+ * @param string $dateTime
199211 * @param string $type
200212 *
201213 * @return bool
202214 */
203215 protected function isValidDateTime ($ dateTime , $ type = "" )
204216 {
205217 $ newDate = DateTime::createFromFormat ($ this ->dateTimeFormat , $ dateTime );
206- if (! $ newDate && $ newDate ->format ($ this ->dateTimeFormat ) == $ dateTime ) {
218+ if (! ( $ newDate && $ newDate ->format ($ this ->dateTimeFormat ) == $ dateTime) ) {
207219 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 );
209222 }
210223 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 );
212226 }
213- throw new InvalidArgumentException ('Invalid date provided ' );
227+ throw new InvalidArgumentException ('Invalid date provided. Date must be of format ' .
228+ $ this ->dateTimeFormat );
214229 }
215230
216231 return true ;
217232 }
218233
234+
219235 /**
220- * Returns date time to string
236+ * Returns correct date time to string
221237 *
222- * @param DateTime $dateTime
238+ * @param string $dateTime
223239 *
224240 * @return string
225241 */
226242 protected function convertEventDateTimeToString ($ dateTime )
227243 {
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 ' );
229250 }
230251}
0 commit comments