TimetableList   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getListUrl() 0 3 1
A getTimetableSelectListType() 0 6 1
A getSelectListValues() 0 15 2
A getTimetableList() 0 11 3
A __construct() 0 4 1
A getTimetableExpandableListType() 0 6 1
A getSubTypeValue() 0 17 2
A getTimetableUrlSubType() 0 6 1
A getTimetableUnorderedListType() 0 17 3
1
<?php
2
3
namespace Wulkanowy\TimetableParser;
4
5
use DOMWrap\Document;
6
use DOMWrap\Element;
7
8
class TimetableList
9
{
10
    private $doc;
11
12
    public function __construct(string $html)
13
    {
14
        $this->doc = new Document();
15
        $this->doc->html($html);
16
    }
17
18
    public function getListUrl()
19
    {
20
        return $this->doc->find('frame[name=list]')->attr('src');
21
    }
22
23
    public function getTimetableList(): array
24
    {
25
        if ($this->doc->find('form[name=form]')->count() > 0) {
26
            return $this->getTimetableSelectListType();
27
        }
28
29
        if ($this->doc->find('body table')->count() > 0) {
30
            return $this->getTimetableExpandableListType();
31
        }
32
33
        return $this->getTimetableUnorderedListType();
34
    }
35
36
    private function getTimetableSelectListType(): array
37
    {
38
        return [
39
            'classes'  => $this->getSelectListValues('oddzialy'),
40
            'teachers' => $this->getSelectListValues('nauczyciele'),
41
            'rooms'    => $this->getSelectListValues('sale'),
42
        ];
43
    }
44
45
    private function getSelectListValues($name): array
46
    {
47
        $nodes = $this->doc->find('[name='.$name.'] option');
48
        $nodes->shift();
49
        $values = [];
50
51
        /** @var Element $class */
52
        foreach ($nodes as $class) {
53
            $values[] = [
54
                'name'  => $class->text(),
55
                'value' => $class->attr('value'),
56
            ];
57
        }
58
59
        return $values;
60
    }
61
62
    private function getTimetableUnorderedListType(): array
63
    {
64
        $teacherQ = 'ul:nth-of-type(2) a';
65
        $roomsQ = 'ul:nth-of-type(3) a';
66
67
        if ($this->doc->find('h4')->count() === 1) {
68
            $teacherQ = 'undefined';
69
            $roomsQ = 'undefined';
70
        } elseif ($this->doc->find('h4:nth-of-type(2)')->text() === 'Sale') {
71
            $teacherQ = 'undefined';
72
            $roomsQ = 'ul:nth-of-type(2) a';
73
        }
74
75
        return $this->getTimetableUrlSubType(
76
            'ul:first-of-type a',
77
            $teacherQ,
78
            $roomsQ
79
        );
80
    }
81
82
    private function getTimetableExpandableListType(): array
83
    {
84
        return $this->getTimetableUrlSubType(
85
            '#oddzialy a',
86
            '#nauczyciele a',
87
            '#sale a'
88
        );
89
    }
90
91
    private function getTimetableUrlSubType(string $classQ, string $teachersQ, string $roomsQ): array
92
    {
93
        return [
94
            'classes'  => $this->getSubTypeValue($classQ, 'o'),
95
            'teachers' => $this->getSubTypeValue($teachersQ, 'n'),
96
            'rooms'    => $this->getSubTypeValue($roomsQ, 's'),
97
        ];
98
    }
99
100
    private function getSubTypeValue(string $query, string $prefix): array
101
    {
102
        $values = [];
103
104
        /* @var Element $class */
105
        foreach ($this->doc->find($query) as $item) {
106
            $values[] = [
107
                'name'  => $item->text(),
108
                'value' => str_replace(
109
                    'plany/'.$prefix,
110
                    '',
111
                    str_replace('.html', '', $item->attr('href'))
112
                ),
113
            ];
114
        }
115
116
        return $values;
117
    }
118
}
119