summaryrefslogtreecommitdiff
path: root/includes/composer/ical/icalEvent.php
blob: a2791c249e7e35fe4e6ceb10096868eefb7b6737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php declare(strict_types=1);

class iCalEvent
{
    public $title;
    public $description;
    public $startDateTime;
    public $endDateTime;
    public $location;
    public $created;
    public $lastModified;

    public function __construct(string $eventContent)
    {
        $this->parseEvent($eventContent);
    }

    protected function parseEvent(string $eventContent) : iCalEvent
    {
        $content = str_replace("\r\n ", '', $eventContent);

        $this->title = $this->getEventDetail($content, "SUMMARY:");
        $this->description = $this->getEventDetail($content, "DESCRIPTION:");
        $this->startDateTime = $this->getEventDateTime($content, "DTSTART");
        $this->endDateTime = $this->getEventDateTime($content, "DTEND");
        $this->location = $this->getEventDetail($content, "LOCATION:");
        $this->created = date('d.m.Y H:i', strtotime($this->getEventDetail($content, "CREATED:")));
        $this->lastModified = date('d.m.Y H:i', strtotime($this->getEventDetail($content, "LAST-MODIFIED:")));

        return $this;
    }

    protected function getEventDetail(string $eventContent, string $eventDetailKey) : string
    {
        $output = "";

        if (preg_match('`^' . $eventDetailKey . '(.*)$`m', $eventContent, $match))
        {
            $output = trim($match[1]);
        }

        return $output;
    }

    protected function getEventDateTime(string $eventContent, string $eventDetailKey) : string
    {
        $output = "";

        if (preg_match('`^' . $eventDetailKey . '(?:;.+)?:([0-9]+(T[0-9]+Z?)?)`m', $eventContent, $match))
        {
            $output = date('d.m.Y H:i', strtotime($match[1]));
        }

        return $output;
    }
}