summaryrefslogtreecommitdiff
path: root/src/Helpers/Schedule/XmlParser.php
blob: 1492aaca85dbfb0ae2b2139575641258989167ed (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

declare(strict_types=1);

namespace Engelsystem\Helpers\Schedule;

use Carbon\Carbon;
use SimpleXMLElement;

class XmlParser
{
    /** @var SimpleXMLElement */
    protected $scheduleXML;

    /** @var Schedule */
    protected $schedule;

    /**
     * @param string $xml
     * @return bool
     */
    public function load(string $xml): bool
    {
        $this->scheduleXML = simplexml_load_string($xml);

        if (!$this->scheduleXML) {
            return false;
        }

        $this->parseXml();

        return true;
    }

    /**
     * Parse the predefined XML content
     */
    protected function parseXml(): void
    {
        $version = $this->getFirstXpathContent('version');
        $conference = new Conference(
            $this->getFirstXpathContent('conference/title'),
            $this->getFirstXpathContent('conference/acronym'),
            $this->getFirstXpathContent('conference/start'),
            $this->getFirstXpathContent('conference/end'),
            (int)$this->getFirstXpathContent('conference/days'),
            $this->getFirstXpathContent('conference/timeslot_duration'),
            $this->getFirstXpathContent('conference/base_url')
        );
        $days = [];

        foreach ($this->scheduleXML->xpath('day') as $day) {
            $rooms = [];

            foreach ($day->xpath('room') as $roomElement) {
                $room = new Room(
                    (string)$roomElement->attributes()['name']
                );

                $events = $this->parseEvents($roomElement->xpath('event'), $room);
                $room->setEvent($events);
                $rooms[] = $room;
            }

            $days[] = new Day(
                (string)$day->attributes()['date'],
                new Carbon($day->attributes()['start']),
                new Carbon($day->attributes()['end']),
                (int)$day->attributes()['index'],
                $rooms
            );
        }

        $this->schedule = new Schedule(
            $version,
            $conference,
            $days
        );
    }

    /**
     * @param SimpleXMLElement[] $eventElements
     * @param Room               $room
     * @return array
     */
    protected function parseEvents(array $eventElements, Room $room): array
    {
        $events = [];

        foreach ($eventElements as $event) {
            $persons = $this->getListFromSequence($event, 'persons', 'person', 'id');
            $links = $this->getListFromSequence($event, 'links', 'link', 'href');
            $attachments = $this->getListFromSequence($event, 'attachments', 'attachment', 'href');

            $recording = '';
            $recordingElement = $event->xpath('recording')[0];
            if ($this->getFirstXpathContent('optout', $recordingElement) == 'false') {
                $recording = $this->getFirstXpathContent('license', $recordingElement);
            }

            $events[] = new Event(
                (string)$event->attributes()['guid'],
                (int)$event->attributes()['id'],
                $room,
                $this->getFirstXpathContent('title', $event),
                $this->getFirstXpathContent('subtitle', $event),
                $this->getFirstXpathContent('type', $event),
                new Carbon($this->getFirstXpathContent('date', $event)),
                $this->getFirstXpathContent('start', $event),
                $this->getFirstXpathContent('duration', $event),
                $this->getFirstXpathContent('abstract', $event),
                $this->getFirstXpathContent('slug', $event),
                $this->getFirstXpathContent('track', $event),
                $this->getFirstXpathContent('logo', $event) ?: null,
                $persons,
                $this->getFirstXpathContent('language', $event) ?: null,
                $this->getFirstXpathContent('description', $event) ?: null,
                $recording,
                $links,
                $attachments,
                $this->getFirstXpathContent('url', $event) ?: null,
                $this->getFirstXpathContent('video_download_url', $event) ?: null
            );
        }

        return $events;
    }

    /**
     * @param string                $path
     * @param SimpleXMLElement|null $xml
     * @return string
     */
    protected function getFirstXpathContent(string $path, ?SimpleXMLElement $xml = null): string
    {
        $element = ($xml ?: $this->scheduleXML)->xpath($path);

        return $element ? (string)$element[0] : '';
    }

    /**
     * Resolves a list from a sequence of elements
     *
     * @param SimpleXMLElement $element
     * @param string           $firstElement
     * @param string           $secondElement
     * @param string           $idAttribute
     * @return array
     */
    protected function getListFromSequence(
        SimpleXMLElement $element,
        string $firstElement,
        string $secondElement,
        string $idAttribute
    ): array {
        $items = [];

        foreach ($element->xpath($firstElement)[0]->xpath($secondElement) as $item) {
            $items[(string)$item->attributes()[$idAttribute]] = (string)$item;
        }

        return $items;
    }

    /**
     * @return Schedule
     */
    public function getSchedule(): Schedule
    {
        return $this->schedule;
    }
}