Completed
Pull Request — master (#37)
by Andreas
03:05
created

Description::parse()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
cc 7
nc 6
nop 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Copyright Andrea Heigl <[email protected]>
6
 *
7
 * Licenses under the MIT-license. For details see the included file LICENSE.md
8
 */
9
10
namespace Callingallpapers\Subcommands\Sessionize\Parser;
11
12
use DOMDocument;
13
use DOMXPath;
14
use function preg_replace;
15
use function str_replace;
16
use function strip_tags;
17
use function strpos;
18
use function var_dump;
19
20
class Description
21
{
22
    public function parse(DOMDocument $dom, DOMXPath $xpath)
23
    {
24
        $result = $xpath->query('//hr[contains(@class, "m-t-none")]');
25
        if ($result->length < 1) {
26
            return '';
27
        }
28
29
        $text = [];
30
        for ($i = 0; $i < $result->length; $i++) {
31
            $currentItem = $result->item($i);
32
            $resultI = $currentItem->parentNode->childNodes;
33
            if ($currentItem->childNodes->length > 0) {
34
                $resultI = $currentItem->childNodes;
35
            }
36
37
            if ($resultI->length <= 0) {
38
                continue;
39
            }
40
            foreach ($resultI as $key => $node) {
41
                $nodeText = trim($dom->saveXML($node));
42
43
                if (false !== strpos($nodeText, 'submit-your-session-button')) {
44
                    continue;
45
                }
46
47
                $nodeText = str_replace('</hr>', '', $nodeText);
48
                $nodeText = str_replace('<hr class="m-t-none">', '', $nodeText);
49
                $text[] = $nodeText;
50
            }
51
        }
52
        $description = trim(implode('', $text));
53
        $description = preg_replace(['/\<\!\-\-.*?\-\-\>/si', '/\<script.*?\<\/script\>/si', '/\<script.*?src=\".*?\/\>/'], '', $description);
54
55
        return $description;
56
    }
57
}
58