Completed
Pull Request — master (#37)
by Andreas
01:34
created

Description::parse()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8177
c 0
b 0
f 0
cc 6
nc 4
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 strip_tags;
16
use function strpos;
17
use function var_dump;
18
19
class Description
20
{
21
    public function parse(DOMDocument $dom, DOMXPath $xpath)
22
    {
23
        $result = $xpath->query('//hr[contains(@class, "m-t-none")]');
24
        if ($result->length < 1) {
25
            return '';
26
        }
27
28
        $text = [];
29
        for ($i = 0; $i < $result->length; $i++) {
30
            $resultI = $result->item($i)->parentNode->childNodes;
31
32
            if ($resultI->length <= 0) {
33
                continue;
34
            }
35
36
            foreach ($resultI as $node) {
37
                $nodeText = trim($dom->saveXML($node));
38
39
                if (0 !== strpos($nodeText, '<hr class="m-t-none">')) {
40
                    continue;
41
                }
42
43
                $text[] = str_replace('<hr class="m-t-none">', '', $nodeText);
44
            }
45
        }
46
        $description = trim(implode('', $text));
47
        $description = preg_replace(['/\<\!\-\-.*?\-\-\>/si', '/\<script.*?\<\/script\>/si'], '', $description);
48
49
        return $description;
50
    }
51
}
52