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

HtmlNode::prepend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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