Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ICalParser | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| parse | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
12 | |||
| parseRRule | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\Parser\Calendar |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\Parser\Calendar; |
| 16 | |
| 17 | /** |
| 18 | * iCal parser. |
| 19 | * |
| 20 | * @package phpOMS\Utils\Parser\Calendar |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class ICalParser |
| 26 | { |
| 27 | /** |
| 28 | * Parse iCal data |
| 29 | * |
| 30 | * @param string $data iCal data |
| 31 | * |
| 32 | * @return array |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | public static function parse(string $data) : array |
| 37 | { |
| 38 | \preg_match_all('/BEGIN:VEVENT(.*?)END:VEVENT/s', $data, $matches, \PREG_SET_ORDER); |
| 39 | |
| 40 | $eventList = []; |
| 41 | |
| 42 | foreach ($matches as $match) { |
| 43 | $event = []; |
| 44 | |
| 45 | \preg_match('/UID:(.*?)\r\n/', $match[1], $uidMatch); |
| 46 | $event['uid'] = \DateTime::createFromFormat('Ymd\THis', $uidMatch[1]); |
| 47 | |
| 48 | \preg_match('/STATUS:(.*?)\r\n/', $match[1], $statusMatch); |
| 49 | $event['status'] = \DateTime::createFromFormat('Ymd\THis', $statusMatch[1]); |
| 50 | |
| 51 | \preg_match('/DTSTART:(.*?)\r\n/', $match[1], $startMatch); |
| 52 | $event['start'] = \DateTime::createFromFormat('Ymd\THis', $startMatch[1]); |
| 53 | |
| 54 | \preg_match('/DTEND:(.*?)\r\n/', $match[1], $endMatch); |
| 55 | $event['end'] = \DateTime::createFromFormat('Ymd\THis', $endMatch[1]); |
| 56 | |
| 57 | \preg_match('/ORGANIZER:(.*?)\r\n/', $match[1], $organizerMatch); |
| 58 | $event['organizer'] = \DateTime::createFromFormat('Ymd\THis', $organizerMatch[1]); |
| 59 | |
| 60 | \preg_match('/SUMMARY:(.*?)\r\n/', $match[1], $summaryMatch); |
| 61 | $event['summary'] = $summaryMatch[1]; |
| 62 | |
| 63 | \preg_match('/DESCRIPTION:(.*?)\r\n/', $match[1], $descriptionMatch); |
| 64 | $event['description'] = $descriptionMatch[1]; |
| 65 | |
| 66 | \preg_match('/LOCATION:(.*?)\r\n/', $match[1], $locationMatch); |
| 67 | $event['location'] = $locationMatch[1]; |
| 68 | |
| 69 | // Check if this event is recurring |
| 70 | if (\preg_match('/RRULE:(.*?)\r\n/', $match[1], $rruleMatch)) { |
| 71 | $rrule = self::parseRRule($rruleMatch[1]); |
| 72 | $event = \array_merge($event, $rrule); |
| 73 | } |
| 74 | |
| 75 | $eventList[] = $event; |
| 76 | } |
| 77 | |
| 78 | return $eventList; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Parse rrule |
| 83 | * |
| 84 | * @param string $rruleString rrule string |
| 85 | * |
| 86 | * @return array |
| 87 | * |
| 88 | * @since 1.0.0 |
| 89 | */ |
| 90 | private static function parseRRule($rruleString) : array |
| 91 | { |
| 92 | $rrule = []; |
| 93 | |
| 94 | \preg_match('/FREQ=(.*?);/', $rruleString, $freqMatch); |
| 95 | $rrule['freq'] = $freqMatch[1]; |
| 96 | |
| 97 | \preg_match('/INTERVAL=(.*?);/', $rruleString, $intervalMatch); |
| 98 | $rrule['interval'] = (int) $intervalMatch[1]; |
| 99 | |
| 100 | if (\preg_match('/COUNT=(.*?);/', $rruleString, $countMatch)) { |
| 101 | $rrule['count'] = (int) $countMatch[1]; |
| 102 | } |
| 103 | |
| 104 | if (\preg_match('/UNTIL=(.*?);/', $rruleString, $untilMatch)) { |
| 105 | $rrule['until'] = \DateTime::createFromFormat('Ymd\THis', $untilMatch[1]); |
| 106 | } |
| 107 | |
| 108 | return $rrule; |
| 109 | } |
| 110 | } |