Passed
Branch scrutinizer (a800ff)
by Thomas
02:24
created

HtmlNode::removeClass()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 0
loc 23
rs 9.7998
c 1
b 1
f 0
1
<?php
2
3
namespace Sulao\HtmlQuery;
4
5
use DOMDocument, DOMNode;
6
7
/**
8
 * Class HtmlNode
9
 *
10
 * @package Sulao\HtmlQuery
11
 */
12
class HtmlNode
13
{
14
    /**
15
     * @var DOMNode
16
     */
17
    protected $node;
18
19
    /**
20
     * HtmlNode constructor.
21
     *
22
     * @param DOMNode $node
23
     */
24
    public function __construct(DOMNode $node)
25
    {
26
        $this->node = $node;
27
    }
28
29
    /**
30
     * Get the outer HTML content.
31
     *
32
     * @return string|null
33
     */
34
    public function outerHtml()
35
    {
36
        return $this->getDoc()->saveHTML($this->node);
37
    }
38
39
    /**
40
     * Get the inner HTML content.
41
     *
42
     * @return string|null
43
     */
44
    public function getHtml()
45
    {
46
        $content = '';
47
        foreach (iterator_to_array($this->node->childNodes) as $childNode) {
48
            $content .= $this->getDoc()->saveHTML($childNode);
49
        }
50
51
        return $content;
52
    }
53
54
    /**
55
     * Get the combined text contents, including it's descendants.
56
     *
57
     * @return string|null
58
     */
59
    public function getText()
60
    {
61
        return $this->node->textContent;
62
    }
63
64
    /**
65
     * Set the text contents.
66
     *
67
     * @param string $text
68
     */
69
    public function setText(string $text)
70
    {
71
        $this->node->nodeValue = $text;
72
    }
73
74
    /**
75
     * Remove all child nodes from the DOM.
76
     */
77
    public function empty()
78
    {
79
        $this->node->nodeValue = '';
80
    }
81
82
    /**
83
     * Remove the node from the DOM.
84
     */
85
    public function remove()
86
    {
87
        if ($this->node->parentNode) {
88
            $this->node->parentNode->removeChild($this->node);
89
        }
90
    }
91
92
    /**
93
     * Insert a node before the node.
94
     *
95
     * @param DOMNode $newNode
96
     */
97
    public function before(DOMNode $newNode)
98
    {
99
        if ($this->node->parentNode) {
100
            $this->node->parentNode->insertBefore($newNode, $this->node);
101
        }
102
    }
103
104
    /**
105
     * Insert new node after the node.
106
     *
107
     * @param DOMNode $newNode
108
     */
109
    public function after(DOMNode $newNode)
110
    {
111
        $nextSibling = $this->node->nextSibling;
112
113
        if ($nextSibling && $nextSibling->parentNode) {
114
            $nextSibling->parentNode->insertBefore($newNode, $nextSibling);
115
        } elseif ($this->node->parentNode) {
116
            $this->node->parentNode->appendChild($newNode);
117
        }
118
    }
119
120
    /**
121
     * Insert a node to the end of the node.
122
     *
123
     * @param DOMNode $newNode
124
     */
125
    public function append(DOMNode $newNode)
126
    {
127
        $this->node->appendChild($newNode);
128
    }
129
130
    /**
131
     * Insert content or node(s) to the beginning of each matched node.
132
     *
133
     * @param DOMNode $newNode
134
     */
135
    public function prepend(DOMNode $newNode)
136
    {
137
        if ($this->node->firstChild) {
138
            $this->node->insertBefore($newNode, $this->node->firstChild);
139
        } else {
140
            $this->node->appendChild($newNode);
141
        }
142
    }
143
144
    /**
145
     * Replace the node with the provided node
146
     *
147
     * @param DOMNode $newNode
148
     */
149
    public function replaceWith(DOMNode $newNode)
150
    {
151
        if ($this->node->parentNode) {
152
            $this->node->parentNode->replaceChild($newNode, $this->node);
153
        }
154
    }
155
156
    /**
157
     * Remove the HTML tag of the node from the DOM.
158
     * Leaving the child nodes in their place.
159
     */
160
    public function unwrapSelf()
161
    {
162
        foreach (iterator_to_array($this->node->childNodes) as $childNode) {
163
            $this->before($childNode);
164
        }
165
166
        $this->remove();
167
    }
168
169
    /**
170
     * Get DOMDocument of the node
171
     *
172
     * @return DOMDocument
173
     */
174
    protected function getDoc(): DOMDocument
175
    {
176
        return $this->node instanceof DOMDocument
177
            ? $this->node
178
            : $this->node->ownerDocument;
179
    }
180
}
181