Completed
Push — master ( 6e010a...c5b995 )
by Thomas
04:09
created

HtmlDocument   A

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