HtmlDocument   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 102
ccs 25
cts 25
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDoc() 0 3 1
A outerHtml() 0 3 1
A find() 0 13 3
A resolve() 0 3 1
A __invoke() 0 3 1
A query() 0 10 2
A __construct() 0 3 1
1
<?php
2
3
namespace Sulao\HtmlQuery;
4
5
use DOMDocument;
6
use DOMNode;
7
8
/**
9
 * Class HtmlDocument
10
 *
11
 * @package Sulao\HtmlQuery
12
 */
13
class HtmlDocument
14
{
15
    /**
16
     * @var DOMDocument
17
     */
18
    protected $doc;
19
20
    /**
21
     * HtmlDocument constructor.
22
     *
23
     * @param DOMDocument $doc
24 81
     */
25
    public function __construct(DOMDocument $doc)
26 81
    {
27 81
        $this->doc = $doc;
28
    }
29
30
    /**
31
     * Get DOMDocument
32
     *
33
     * @return DOMDocument
34 2
     */
35
    public function getDoc(): DOMDocument
36 2
    {
37
        return $this->doc;
38
    }
39
40
    /**
41
     * Get the outer HTML content.
42
     *
43
     * @return string|null
44 19
     */
45
    public function outerHtml()
46 19
    {
47
        return $this->doc->saveHTML();
48
    }
49
50
    /**
51
     *  Make the static object can be called as a function.
52
     *
53
     * @param string $selector
54
     *
55
     * @return HtmlQuery
56 31
     */
57
    public function __invoke(string $selector)
58 31
    {
59
        return $this->query($selector);
60
    }
61
62
    /**
63
     * If the parameter is raw html, then create document fragment for it,
64
     * If the parameter is a css selector, get the descendants
65
     * filtered by a css selector.
66
     *
67
     * @param string $selector css selector or raw html
68
     *
69
     * @return HtmlQuery
70 36
     */
71
    public function query(string $selector)
72 36
    {
73 7
        if (Helper::isRawHtml($selector)) {
74 7
            $frag = $this->doc->createDocumentFragment();
75
            $frag->appendXML($selector);
76 7
77
            return $this->resolve($frag);
78
        }
79 34
80
        return $this->find($selector);
81
    }
82
83
    /**
84
     * Get the descendants of document, filtered by a selector.
85
     *
86
     * @param string $selector
87
     *
88
     * @return HtmlQuery
89 77
     */
90
    public function find(string $selector)
91 77
    {
92 77
        $nodes = Helper::xpathQuery(
93 77
            Helper::toXpath($selector),
94 77
            $this->doc,
95
            $this->doc
96
        );
97 77
98 4
        if (Helper::isIdSelector($selector)) {
99
            $nodes = $nodes ? $nodes[0] : [];
100
        }
101 77
102
        return $this->resolve($nodes);
103
    }
104
105
    /**
106
     * Resolve nodes to HtmlQuery instance.
107
     *
108
     * @param DOMNode|DOMNode[] $nodes
109
     *
110
     * @return HtmlQuery
111 78
     */
112
    protected function resolve($nodes)
113 78
    {
114
        return new HtmlQuery($this->doc, $nodes);
115
    }
116
}
117