PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / src / Utils / DateHelper.php
ameliabooking / vendor / apimatic / core / src / Utils Last commit date
CoreHelper.php 1 year ago DateHelper.php 1 year ago JsonHelper.php 1 year ago XmlDeserializer.php 1 year ago XmlSerializer.php 1 year ago
DateHelper.php
631 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Core\Utils;
6
7 use DateTime;
8 use DateTimeZone;
9 use InvalidArgumentException;
10 use stdClass;
11
12 class DateHelper
13 {
14 /**
15 * Match the pattern for a datetime string in simple date format
16 */
17 public const SIMPLE_DATE = 'Y-m-d';
18
19 /**
20 * Match the pattern for a datetime string in Rfc1123 format
21 */
22 public const RFC1123 = 'D, d M Y H:i:s T';
23
24 /**
25 * Match the pattern for a datetime string in RFC3339 format
26 */
27 public const RFC3339 = 'Y-m-d\TH:i:sP';
28
29 /**
30 * Convert a DateTime object to a string in simple date format
31 *
32 * @param DateTime|null $date The DateTime object to convert
33 *
34 * @return string|null The datetime as a string in simple date format
35 * @throws InvalidArgumentException
36 */
37 public static function toSimpleDate(?DateTime $date): ?string
38 {
39 if (is_null($date)) {
40 return null;
41 }
42 return $date->format(static::SIMPLE_DATE);
43 }
44
45 /**
46 * Convert an array of DateTime objects to an array of strings in simple date format
47 *
48 * @param array|null $dates The array of DateTime objects to convert
49 *
50 * @return array|null The array of datetime strings in simple date format
51 */
52 public static function toSimpleDateArray(?array $dates): ?array
53 {
54 if (is_null($dates)) {
55 return null;
56 }
57 return array_map([self::class, 'toSimpleDate'], $dates);
58 }
59
60 /**
61 * Convert a 2D array of DateTime objects to a 2D array of strings in simple date format
62 *
63 * @param array|null $dates The 2D array of DateTime objects to convert
64 *
65 * @return array|null The 2D array of datetime strings in simple date format
66 */
67 public static function toSimpleDate2DArray(?array $dates): ?array
68 {
69 if (is_null($dates)) {
70 return null;
71 }
72 return array_map([self::class, 'toSimpleDateArray'], $dates);
73 }
74
75 /**
76 * Parse a datetime string in simple date format to a DateTime object
77 *
78 * @param string|null $date A datetime string in simple date format
79 *
80 * @return DateTime|null The parsed DateTime object
81 * @throws InvalidArgumentException
82 */
83 public static function fromSimpleDate(?string $date): ?DateTime
84 {
85 if (is_null($date)) {
86 return null;
87 }
88 $x = DateTime::createFromFormat(static::SIMPLE_DATE, $date);
89 if ($x instanceof DateTime) {
90 return $x->setTime(0, 0);
91 }
92 throw new InvalidArgumentException('Incorrect format.');
93 }
94
95 /**
96 * Parse a datetime string in simple date format to a DateTime object
97 *
98 * @param string|null $date A datetime string in simple date format
99 *
100 * @return DateTime The parsed DateTime object
101 * @throws InvalidArgumentException
102 */
103 public static function fromSimpleDateRequired(?string $date): DateTime
104 {
105 $result = DateHelper::fromSimpleDate($date);
106
107 if (isset($result)) {
108 return $result;
109 }
110
111 throw new \InvalidArgumentException('Date is null, empty or not in required format.');
112 }
113
114 /**
115 * Parse an array of datetime strings in simple date format to an array of DateTime objects
116 *
117 * @param array|null $dates An array of datetime strings in simple date format
118 *
119 * @return array|null An array of parsed DateTime objects
120 */
121 public static function fromSimpleDateArray(?array $dates): ?array
122 {
123 if (is_null($dates)) {
124 return null;
125 }
126 return array_map([self::class, 'fromSimpleDate'], $dates);
127 }
128
129 /**
130 * Parse an array of map of datetime strings in simple date format to a 2D array of DateTime objects
131 *
132 * @param array|null $dates An array of map of datetime strings in simple date format
133 *
134 * @return array|null A 2D array of parsed DateTime objects
135 */
136 public static function fromSimpleDateArrayOfMap(?array $dates): ?array
137 {
138 if (is_null($dates)) {
139 return null;
140 }
141 return array_map([self::class, 'fromSimpleDateMap'], $dates);
142 }
143
144 /**
145 * Parse a class of datetime strings in simple date format to an array of DateTime objects
146 *
147 * @param stdClass|null $dates A class of datetime strings in simple date format
148 *
149 * @return array|null An array of parsed DateTime objects
150 */
151 public static function fromSimpleDateMap(?stdClass $dates): ?array
152 {
153 if (is_null($dates)) {
154 return null;
155 }
156 $array = json_decode(json_encode($dates), true);
157 return array_map([self::class, 'fromSimpleDate'], $array);
158 }
159
160 /**
161 * Parse a map of array of datetime strings in simple date format to a 2D array of DateTime objects
162 *
163 * @param stdClass|null $dates A map of array of datetime strings in simple date format
164 *
165 * @return array|null A 2D array of parsed DateTime objects
166 */
167 public static function fromSimpleDateMapOfArray(?stdClass $dates): ?array
168 {
169 if (is_null($dates)) {
170 return null;
171 }
172 $array = json_decode(json_encode($dates), true);
173 return array_map([self::class, 'fromSimpleDateArray'], $array);
174 }
175
176 /**
177 * Convert a DateTime object to a string in Rfc1123 format
178 *
179 * @param DateTime|null $date The DateTime object to convert
180 *
181 * @return string|null The datetime as a string in Rfc1123 format
182 * @throws InvalidArgumentException
183 */
184 public static function toRfc1123DateTime(?DateTime $date): ?string
185 {
186 if (is_null($date)) {
187 return null;
188 }
189 return $date->setTimeZone(new DateTimeZone('GMT'))->format(static::RFC1123);
190 }
191
192 /**
193 * Convert an array of DateTime objects to an array of strings in Rfc1123 format
194 *
195 * @param array|null $dates The array of DateTime objects to convert
196 *
197 * @return array|null The array of datetime strings in Rfc1123 format
198 */
199 public static function toRfc1123DateTimeArray(?array $dates): ?array
200 {
201 if (is_null($dates)) {
202 return null;
203 }
204 return array_map([self::class, 'toRfc1123DateTime'], $dates);
205 }
206
207 /**
208 * Convert a 2D array of DateTime objects to a 2D array of strings in Rfc1123 format
209 *
210 * @param array|null $dates The 2D array of DateTime objects to convert
211 *
212 * @return array|null The 2D array of datetime strings in Rfc1123 format
213 */
214 public static function toRfc1123DateTime2DArray(?array $dates): ?array
215 {
216 if (is_null($dates)) {
217 return null;
218 }
219 return array_map([self::class, 'toRfc1123DateTimeArray'], $dates);
220 }
221
222 /**
223 * Parse a datetime string in Rfc1123 format to a DateTime object
224 *
225 * @param string|null $date A datetime string in Rfc1123 format
226 *
227 * @return DateTime|null The parsed DateTime object
228 * @throws InvalidArgumentException
229 */
230 public static function fromRfc1123DateTime(?string $date): ?DateTime
231 {
232 if (is_null($date)) {
233 return null;
234 }
235 $x = DateTime::createFromFormat(static::RFC1123, $date);
236 if ($x instanceof DateTime) {
237 return $x->setTimeZone(new DateTimeZone('GMT'));
238 }
239 throw new InvalidArgumentException('Incorrect format.');
240 }
241
242 /**
243 * Parse a datetime string in Rfc1123 format to a DateTime object
244 *
245 * @param string|null $datetime A datetime string in Rfc1123 format
246 *
247 * @return DateTime The parsed DateTime object
248 * @throws InvalidArgumentException
249 */
250 public static function fromRfc1123DateTimeRequired(?string $datetime): DateTime
251 {
252 $result = DateHelper::fromRfc1123DateTime($datetime);
253
254 if (isset($result)) {
255 return $result;
256 }
257
258 throw new \InvalidArgumentException('DateTime is null, empty or not in required format.');
259 }
260
261 /**
262 * Parse an array of datetime strings in Rfc1123 format to an array of DateTime objects
263 *
264 * @param array|null $dates An array of datetime strings in Rfc1123 format
265 *
266 * @return array|null An array of parsed DateTime objects
267 */
268 public static function fromRfc1123DateTimeArray(?array $dates): ?array
269 {
270 if (is_null($dates)) {
271 return null;
272 }
273 return array_map([self::class, 'fromRfc1123DateTime'], $dates);
274 }
275
276 /**
277 * Parse an array of map of datetime strings in Rfc1123 format to a 2D array of DateTime objects
278 *
279 * @param array|null $dates An array of map of datetime strings in Rfc1123 format
280 *
281 * @return array|null A 2D array of parsed DateTime objects
282 */
283 public static function fromRfc1123DateTimeArrayOfMap(?array $dates): ?array
284 {
285 if (is_null($dates)) {
286 return null;
287 }
288 return array_map([self::class, 'fromRfc1123DateTimeMap'], $dates);
289 }
290
291 /**
292 * Parse a class of datetime strings in Rfc1123 format to an array of DateTime objects
293 *
294 * @param stdClass|null $dates A class of datetime strings in Rfc1123 format
295 *
296 * @return array|null An array of parsed DateTime objects
297 */
298 public static function fromRfc1123DateTimeMap(?stdClass $dates): ?array
299 {
300 if (is_null($dates)) {
301 return null;
302 }
303 $array = json_decode(json_encode($dates), true);
304 return array_map([self::class, 'fromRfc1123DateTime'], $array);
305 }
306
307 /**
308 * Parse a map of array of datetime strings in Rfc1123 format to a 2D array of DateTime objects
309 *
310 * @param stdClass|null $dates A map of array of datetime strings in Rfc1123 format
311 *
312 * @return array|null A 2D array of parsed DateTime objects
313 */
314 public static function fromRfc1123DateTimeMapOfArray(?stdClass $dates): ?array
315 {
316 if (is_null($dates)) {
317 return null;
318 }
319 $array = json_decode(json_encode($dates), true);
320 return array_map([self::class, 'fromRfc1123DateTimeArray'], $array);
321 }
322
323 /**
324 * Convert a DateTime object to a string in Rfc3339 format
325 *
326 * @param DateTime|null $date The DateTime object to convert
327 *
328 * @return string|null The datetime as a string in Rfc3339 format
329 * @throws InvalidArgumentException
330 */
331 public static function toRfc3339DateTime(?DateTime $date): ?string
332 {
333 if (is_null($date)) {
334 return null;
335 }
336 return $date->setTimeZone(new DateTimeZone('UTC'))->format(static::RFC3339);
337 }
338
339 /**
340 * Convert an array of DateTime objects to an array of strings in Rfc3339 format
341 *
342 * @param array|null $dates The array of DateTime objects to convert
343 *
344 * @return array|null The array of datetime strings in Rfc3339 format
345 */
346 public static function toRfc3339DateTimeArray(?array $dates): ?array
347 {
348 if (is_null($dates)) {
349 return null;
350 }
351 return array_map([self::class, 'toRfc3339DateTime'], $dates);
352 }
353
354 /**
355 * Convert a 2D array of DateTime objects to a 2D array of strings in Rfc3339 format
356 *
357 * @param array|null $dates The 2D array of DateTime objects to convert
358 *
359 * @return array|null The 2D array of datetime strings in Rfc3339 format
360 */
361 public static function toRfc3339DateTime2DArray(?array $dates): ?array
362 {
363 if (is_null($dates)) {
364 return null;
365 }
366 return array_map([self::class, 'toRfc3339DateTimeArray'], $dates);
367 }
368
369 /**
370 * Parse a datetime string in Rfc3339 format to a DateTime object
371 *
372 * @param string|null $date A datetime string in Rfc3339 format
373 *
374 * @return DateTime|null The parsed DateTime object
375 * @throws InvalidArgumentException
376 */
377 public static function fromRfc3339DateTime(?string $date): ?DateTime
378 {
379 if (is_null($date)) {
380 return null;
381 }
382
383 // Check for timezone information and append it if missing
384 if (empty(preg_match("/T.*[+-]|T.*Z/", $date))) {
385 $date .= 'Z';
386 }
387
388 $x = DateTime::createFromFormat(static::RFC3339, $date);
389 if ($x instanceof DateTime) {
390 return $x->setTimeZone(new DateTimeZone('UTC'));
391 }
392 $x = DateTime::createFromFormat("Y-m-d\TH:i:s.uP", $date); // parse with up to 6 microseconds
393 if ($x instanceof DateTime) {
394 return $x->setTimeZone(new DateTimeZone('UTC'));
395 }
396 $x = DateTime::createFromFormat("Y-m-d\TH:i:s.uuP", $date); // parse with up to 12 microseconds
397 if ($x instanceof DateTime) {
398 return $x->setTimeZone(new DateTimeZone('UTC'));
399 }
400 throw new InvalidArgumentException('Incorrect format.');
401 }
402
403 /**
404 * Parse a datetime string in Rfc3339 format to a DateTime object
405 *
406 * @param string|null $datetime A datetime string in Rfc3339 format
407 *
408 * @return DateTime The parsed DateTime object
409 * @throws InvalidArgumentException
410 */
411 public static function fromRfc3339DateTimeRequired(?string $datetime): DateTime
412 {
413 $result = DateHelper::fromRfc3339DateTime($datetime);
414
415 if (isset($result)) {
416 return $result;
417 }
418
419 throw new \InvalidArgumentException('DateTime is null, empty or not in required format.');
420 }
421
422 /**
423 * Parse an array of datetime strings in Rfc3339 format to an array of DateTime objects
424 *
425 * @param array|null $dates An array of datetime strings in Rfc3339 format
426 *
427 * @return array|null An array of parsed DateTime objects
428 */
429 public static function fromRfc3339DateTimeArray(?array $dates): ?array
430 {
431 if (is_null($dates)) {
432 return null;
433 }
434 return array_map([self::class, 'fromRfc3339DateTime'], $dates);
435 }
436
437 /**
438 * Parse an array of map of datetime strings in Rfc3339 format to a 2D array DateTime objects
439 *
440 * @param array|null $dates An array of map of datetime strings in Rfc3339 format
441 *
442 * @return array|null A 2D array of parsed DateTime objects
443 */
444 public static function fromRfc3339DateTimeArrayOfMap(?array $dates): ?array
445 {
446 if (is_null($dates)) {
447 return null;
448 }
449 return array_map([self::class, 'fromRfc3339DateTimeMap'], $dates);
450 }
451
452 /**
453 * Parse a class of datetime strings in Rfc3339 format to an array of DateTime objects
454 *
455 * @param stdClass|null $dates A class of datetime strings in Rfc3339 format
456 *
457 * @return array|null An array of parsed DateTime objects
458 */
459 public static function fromRfc3339DateTimeMap(?stdClass $dates): ?array
460 {
461 if (is_null($dates)) {
462 return null;
463 }
464 $array = json_decode(json_encode($dates), true);
465 return array_map([self::class, 'fromRfc3339DateTime'], $array);
466 }
467
468 /**
469 * Parse a map of array of datetime strings in Rfc3339 format to a 2D array of DateTime objects
470 *
471 * @param stdClass|null $dates A map of array of datetime strings in Rfc3339 format
472 *
473 * @return array|null A 2D array of parsed DateTime objects
474 */
475 public static function fromRfc3339DateTimeMapOfArray(?stdClass $dates): ?array
476 {
477 if (is_null($dates)) {
478 return null;
479 }
480 $array = json_decode(json_encode($dates), true);
481 return array_map([self::class, 'fromRfc3339DateTimeArray'], $array);
482 }
483
484 /**
485 * Convert a DateTime object to a Unix Timestamp
486 *
487 * @param DateTime|null $date The DateTime object to convert
488 *
489 * @return int|null The converted Unix Timestamp
490 * @throws InvalidArgumentException
491 */
492 public static function toUnixTimestamp(?DateTime $date): ?int
493 {
494 if (is_null($date)) {
495 return null;
496 }
497 return $date->getTimestamp();
498 }
499
500 /**
501 * Convert an array of DateTime objects to an array of Unix timestamps
502 *
503 * @param array|null $dates The array of DateTime objects to convert
504 *
505 * @return array|null The array of integers representing date-time in Unix timestamp
506 */
507 public static function toUnixTimestampArray(?array $dates): ?array
508 {
509 if (is_null($dates)) {
510 return null;
511 }
512 return array_map([self::class, 'toUnixTimestamp'], $dates);
513 }
514
515 /**
516 * Convert a 2D array of DateTime objects to a 2D array of Unix timestamps
517 *
518 * @param array|null $dates The 2D array of DateTime objects to convert
519 *
520 * @return array|null The 2D array of integers representing date-time in Unix timestamp
521 */
522 public static function toUnixTimestamp2DArray(?array $dates): ?array
523 {
524 if (is_null($dates)) {
525 return null;
526 }
527 return array_map([self::class, 'toUnixTimestampArray'], $dates);
528 }
529
530 /**
531 * Parse a Unix Timestamp to a DateTime object
532 *
533 * @param string|null $date The Unix Timestamp
534 *
535 * @return DateTime|null The parsed DateTime object
536 * @throws InvalidArgumentException
537 */
538 public static function fromUnixTimestamp(?string $date): ?DateTime
539 {
540 if (empty($date)) {
541 return null;
542 }
543 $x = DateTime::createFromFormat("U", $date);
544 if ($x instanceof DateTime) {
545 return $x;
546 }
547 throw new InvalidArgumentException('Incorrect format.');
548 }
549
550 /**
551 * Parse a Unix Timestamp to a DateTime object
552 *
553 * @param string|null $datetime The Unix Timestamp
554 *
555 * @return DateTime The parsed DateTime object
556 * @throws InvalidArgumentException
557 */
558 public static function fromUnixTimestampRequired(?string $datetime): DateTime
559 {
560 $result = DateHelper::fromUnixTimestamp($datetime);
561
562 if (isset($result)) {
563 return $result;
564 }
565
566 throw new \InvalidArgumentException('DateTime is null, empty or not in required format.');
567 }
568
569 /**
570 * Parse an array of Unix Timestamps to an array of DateTime objects
571 *
572 * @param array|null $dates An array of Unix Timestamps
573 *
574 * @return array|null An array of parsed DateTime objects
575 */
576 public static function fromUnixTimestampArray(?array $dates): ?array
577 {
578 if (is_null($dates)) {
579 return null;
580 }
581 return array_map([self::class, 'fromUnixTimestamp'], array_map('strval', $dates));
582 }
583
584 /**
585 * Parse an array of map of Unix Timestamps to a 2D array of DateTime objects
586 *
587 * @param array|null $dates An array of map of Unix Timestamps
588 *
589 * @return array|null A 2D array of parsed DateTime objects
590 */
591 public static function fromUnixTimestampArrayOfMap(?array $dates): ?array
592 {
593 if (is_null($dates)) {
594 return null;
595 }
596 return array_map([self::class, 'fromUnixTimestampMap'], $dates);
597 }
598
599 /**
600 * Parse a class of Unix Timestamps to an array of DateTime objects
601 *
602 * @param stdClass|null $dates A class of Unix Timestamps
603 *
604 * @return array|null An array of parsed DateTime objects
605 */
606 public static function fromUnixTimestampMap(?stdClass $dates): ?array
607 {
608 if (is_null($dates)) {
609 return null;
610 }
611 $array = json_decode(json_encode($dates), true);
612 return array_map([self::class, 'fromUnixTimestamp'], array_map('strval', $array));
613 }
614
615 /**
616 * Parse a map of array of Unix Timestamps to a 2D array of DateTime objects
617 *
618 * @param stdClass|null $dates A map of array of Unix Timestamps
619 *
620 * @return array|null A 2D array of parsed DateTime objects
621 */
622 public static function fromUnixTimestampMapOfArray(?stdClass $dates): ?array
623 {
624 if (is_null($dates)) {
625 return null;
626 }
627 $array = json_decode(json_encode($dates), true);
628 return array_map([self::class, 'fromUnixTimestampArray'], $array);
629 }
630 }
631