@@ -12,7 +12,10 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\PrototypeDOM; |
14 | 14 | |
15 | -use DOMDocument,DOMNode, DOMNodeList, DOMXPath; |
|
15 | +use DOMDocument; |
|
16 | +use DOMNode; |
|
17 | +use DOMNodeList; |
|
18 | +use DOMXPath; |
|
16 | 19 | use Symfony\Component\CssSelector\CssSelectorConverter; |
17 | 20 | |
18 | 21 | class Document extends DOMDocument{ |
@@ -12,10 +12,10 @@ discard block |
||
12 | 12 | |
13 | 13 | namespace chillerlan\PrototypeDOM; |
14 | 14 | |
15 | -use DOMDocument,DOMNode, DOMNodeList, DOMXPath; |
|
15 | +use DOMDocument, DOMNode, DOMNodeList, DOMXPath; |
|
16 | 16 | use Symfony\Component\CssSelector\CssSelectorConverter; |
17 | 17 | |
18 | -class Document extends DOMDocument{ |
|
18 | +class Document extends DOMDocument { |
|
19 | 19 | |
20 | 20 | const NODE_CLASSES = [ |
21 | 21 | 'Attr', |
@@ -34,10 +34,10 @@ discard block |
||
34 | 34 | * @param string|null $version |
35 | 35 | * @param string|null $encoding |
36 | 36 | */ |
37 | - public function __construct(string $content = null, $version = '1.0', $encoding = 'UTF-8'){ |
|
37 | + public function __construct(string $content = null, $version = '1.0', $encoding = 'UTF-8') { |
|
38 | 38 | parent::__construct($version, $encoding); |
39 | 39 | |
40 | - foreach(self::NODE_CLASSES as $nodeClass){ |
|
40 | + foreach (self::NODE_CLASSES as $nodeClass) { |
|
41 | 41 | $this->registerNodeClass('DOM'.$nodeClass, __NAMESPACE__.'\\'.$nodeClass); |
42 | 42 | } |
43 | 43 | |
@@ -84,21 +84,21 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function select($selectors = null, DOMNode $contextNode = null, string $axis = 'descendant-or-self::', int $nodeType = XML_ELEMENT_NODE):NodeList{ |
86 | 86 | |
87 | - if(is_string($selectors)){ |
|
87 | + if (is_string($selectors)) { |
|
88 | 88 | $selectors = [trim($selectors)]; |
89 | 89 | } |
90 | 90 | |
91 | - if(!is_array($selectors) || empty($selectors)){ |
|
91 | + if (!is_array($selectors) || empty($selectors)) { |
|
92 | 92 | $selectors = ['*']; |
93 | 93 | } |
94 | 94 | |
95 | 95 | $elements = new NodeList; |
96 | 96 | |
97 | - foreach($selectors as $selector){ |
|
97 | + foreach ($selectors as $selector) { |
|
98 | 98 | |
99 | - foreach($this->querySelectorAll($selector, $contextNode, $axis) as $element){ |
|
99 | + foreach ($this->querySelectorAll($selector, $contextNode, $axis) as $element) { |
|
100 | 100 | |
101 | - if($element->nodeType === $nodeType){ |
|
101 | + if ($element->nodeType === $nodeType) { |
|
102 | 102 | $elements[] = $element; |
103 | 103 | } |
104 | 104 | |
@@ -144,9 +144,9 @@ discard block |
||
144 | 144 | public function removeElementsBySelector($selectors, DOMNode $contextNode = null, string $axis = 'descendant-or-self::'):Document{ |
145 | 145 | $nodes = $this->select($selectors, $contextNode, $axis); |
146 | 146 | |
147 | - if(count($nodes) > 0){ |
|
147 | + if (count($nodes) > 0) { |
|
148 | 148 | /** @var \chillerlan\PrototypeDOM\Element $node */ |
149 | - foreach($nodes as $node){ |
|
149 | + foreach ($nodes as $node) { |
|
150 | 150 | $node->remove(); |
151 | 151 | } |
152 | 152 | |
@@ -163,16 +163,16 @@ discard block |
||
163 | 163 | */ |
164 | 164 | public function _toNodeList($content):NodeList{ |
165 | 165 | |
166 | - if($content instanceof NodeList || $content instanceof DOMNodeList || is_array($content)){ |
|
166 | + if ($content instanceof NodeList || $content instanceof DOMNodeList || is_array($content)) { |
|
167 | 167 | return new NodeList($content); |
168 | 168 | } |
169 | - elseif($content instanceof DOMNode){ |
|
169 | + elseif ($content instanceof DOMNode) { |
|
170 | 170 | return new NodeList([$content]); |
171 | 171 | } |
172 | - elseif(is_string($content)){ |
|
172 | + elseif (is_string($content)) { |
|
173 | 173 | return $this->_loadHTMLFragment($content); |
174 | 174 | } |
175 | - else{ |
|
175 | + else { |
|
176 | 176 | throw new \Exception('invalid content'); // @codeCoverageIgnore |
177 | 177 | } |
178 | 178 | |
@@ -189,15 +189,15 @@ discard block |
||
189 | 189 | public function recursivelyCollect(DOMNode $element, string $property, int $maxLength = -1, int $nodeType = XML_ELEMENT_NODE):NodeList{ |
190 | 190 | $nodes = new NodeList; |
191 | 191 | |
192 | - if(in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])){ |
|
192 | + if (in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])) { |
|
193 | 193 | |
194 | - while($element = $element->{$property}){ |
|
194 | + while ($element = $element->{$property}) { |
|
195 | 195 | |
196 | - if($element->nodeType === $nodeType){ |
|
196 | + if ($element->nodeType === $nodeType) { |
|
197 | 197 | $nodes[] = $element; |
198 | 198 | } |
199 | 199 | |
200 | - if(count($nodes) === $maxLength){ |
|
200 | + if (count($nodes) === $maxLength) { |
|
201 | 201 | break; |
202 | 202 | } |
203 | 203 | |
@@ -217,14 +217,14 @@ discard block |
||
217 | 217 | * |
218 | 218 | * @return \DOMNode|null |
219 | 219 | */ |
220 | - public function _recursivelyFind(DOMNode $element, string $property, string $selector = null, int $index = 0, int $nodeType = XML_ELEMENT_NODE){ |
|
220 | + public function _recursivelyFind(DOMNode $element, string $property, string $selector = null, int $index = 0, int $nodeType = XML_ELEMENT_NODE) { |
|
221 | 221 | |
222 | - if(in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])){ |
|
222 | + if (in_array($property, ['parentNode', 'previousSibling', 'nextSibling'])) { |
|
223 | 223 | |
224 | 224 | /** @var \chillerlan\PrototypeDOM\Element $element */ |
225 | - while($element = $element->{$property}){ |
|
225 | + while ($element = $element->{$property}) { |
|
226 | 226 | |
227 | - if($element->nodeType !== $nodeType || !is_null($selector) && !$element->match($selector) || --$index >= 0){ |
|
227 | + if ($element->nodeType !== $nodeType || !is_null($selector) && !$element->match($selector) || --$index >= 0) { |
|
228 | 228 | continue; |
229 | 229 | } |
230 | 230 | |
@@ -244,9 +244,9 @@ discard block |
||
244 | 244 | */ |
245 | 245 | public function match(DOMNode $element, string $selector):bool{ |
246 | 246 | |
247 | - foreach($this->select($selector) as $match){ |
|
247 | + foreach ($this->select($selector) as $match) { |
|
248 | 248 | |
249 | - if($element->isSameNode($match)){ |
|
249 | + if ($element->isSameNode($match)) { |
|
250 | 250 | return true; |
251 | 251 | } |
252 | 252 | |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | /** @var \chillerlan\PrototypeDOM\Element $element */ |
266 | 266 | $element = $this->createElement($tag); |
267 | 267 | |
268 | - if($attributes){ |
|
268 | + if ($attributes) { |
|
269 | 269 | $element->setAttributes($attributes); |
270 | 270 | } |
271 | 271 |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * @extends \DOMNode |
19 | 19 | */ |
20 | -trait ManipulationTrait{ |
|
20 | +trait ManipulationTrait { |
|
21 | 21 | |
22 | 22 | /** |
23 | 23 | * @link http://php.net/manual/class.domnode.php#domnode.props.ownerdocument |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function remove():DOMNode{ |
48 | 48 | |
49 | - if(!$this->parentNode){ |
|
49 | + if (!$this->parentNode) { |
|
50 | 50 | /** @var \DOMNode $this */ |
51 | 51 | return $this; |
52 | 52 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function replace(DOMNode $newnode):DOMNode{ |
64 | 64 | |
65 | - if(!$this->parentNode){ |
|
65 | + if (!$this->parentNode) { |
|
66 | 66 | /** @var \DOMNode $this */ |
67 | 67 | return $this; |
68 | 68 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public function empty():DOMNode{ |
88 | 88 | |
89 | - while($this->hasChildNodes()){ |
|
89 | + while ($this->hasChildNodes()) { |
|
90 | 90 | $this->firstChild->remove(); |
91 | 91 | } |
92 | 92 | |
@@ -113,10 +113,10 @@ discard block |
||
113 | 113 | public function cleanWhitespace():DOMNode{ |
114 | 114 | $node = $this->firstChild; |
115 | 115 | |
116 | - while($node){ |
|
116 | + while ($node) { |
|
117 | 117 | $nextNode = $node->nextSibling; |
118 | 118 | |
119 | - if($node->nodeType === XML_TEXT_NODE && empty(trim($node->nodeValue))){ |
|
119 | + if ($node->nodeType === XML_TEXT_NODE && empty(trim($node->nodeValue))) { |
|
120 | 120 | $node->remove(); |
121 | 121 | } |
122 | 122 | |
@@ -149,18 +149,18 @@ discard block |
||
149 | 149 | */ |
150 | 150 | public function insert($content):DOMNode{ |
151 | 151 | |
152 | - if(is_array($content)){ |
|
152 | + if (is_array($content)) { |
|
153 | 153 | |
154 | - foreach(['before', 'after', 'top', 'bottom'] as $pos){ |
|
154 | + foreach (['before', 'after', 'top', 'bottom'] as $pos) { |
|
155 | 155 | |
156 | - if(array_key_exists($pos, $content)){ |
|
156 | + if (array_key_exists($pos, $content)) { |
|
157 | 157 | $nodes = $this->ownerDocument->_toNodeList($content[$pos]); |
158 | 158 | |
159 | - if($pos === 'top' && $this->hasChildNodes() || $pos === 'after' && $this->nextSibling){ |
|
159 | + if ($pos === 'top' && $this->hasChildNodes() || $pos === 'after' && $this->nextSibling) { |
|
160 | 160 | $nodes->reverse(); |
161 | 161 | } |
162 | 162 | |
163 | - foreach($nodes as $node){ |
|
163 | + foreach ($nodes as $node) { |
|
164 | 164 | call_user_func_array([$this, 'insert_'.$pos], [$node]); |
165 | 165 | } |
166 | 166 | |
@@ -169,8 +169,8 @@ discard block |
||
169 | 169 | } |
170 | 170 | |
171 | 171 | } |
172 | - else{ |
|
173 | - foreach($this->ownerDocument->_toNodeList($content) as $node){ |
|
172 | + else { |
|
173 | + foreach ($this->ownerDocument->_toNodeList($content) as $node) { |
|
174 | 174 | $this->insert_bottom($node); |
175 | 175 | } |
176 | 176 | } |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | */ |
188 | 188 | public function insert_before(DOMNode $node, DOMNode $refNode = null):DOMNode{ |
189 | 189 | |
190 | - if($this->parentNode){ |
|
190 | + if ($this->parentNode) { |
|
191 | 191 | $this->parentNode->insertBefore($this->_importNode($node), $refNode ?? $this); |
192 | 192 | } |
193 | 193 |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * @extends \DOMNode |
19 | 19 | */ |
20 | -trait TraversalTrait{ |
|
20 | +trait TraversalTrait { |
|
21 | 21 | |
22 | 22 | /** |
23 | 23 | * @link http://php.net/manual/class.domnode.php#domnode.props.ownerdocument |
@@ -46,9 +46,9 @@ discard block |
||
46 | 46 | * |
47 | 47 | * @return \DOMNode|null |
48 | 48 | */ |
49 | - public function _recursivelyFind($selector, $index, string $property, int $nodeType = XML_ELEMENT_NODE){ |
|
49 | + public function _recursivelyFind($selector, $index, string $property, int $nodeType = XML_ELEMENT_NODE) { |
|
50 | 50 | |
51 | - if(is_numeric($selector)){ |
|
51 | + if (is_numeric($selector)) { |
|
52 | 52 | $index = $selector; |
53 | 53 | $selector = null; |
54 | 54 | } |
@@ -91,13 +91,13 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @return \DOMNode|null |
93 | 93 | */ |
94 | - public function down($expression = null, int $index = 0){ |
|
94 | + public function down($expression = null, int $index = 0) { |
|
95 | 95 | |
96 | - if(count(func_get_args()) === 0){ |
|
96 | + if (count(func_get_args()) === 0) { |
|
97 | 97 | return $this->firstDescendant(); |
98 | 98 | } |
99 | 99 | |
100 | - if(is_numeric($expression)){ |
|
100 | + if (is_numeric($expression)) { |
|
101 | 101 | $index = $expression; |
102 | 102 | $expression = '*'; |
103 | 103 | } |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @return \DOMNode|null |
113 | 113 | */ |
114 | - public function up($expression = null, int $index = null){ |
|
114 | + public function up($expression = null, int $index = null) { |
|
115 | 115 | return $this->_recursivelyFind($expression, $index, 'parentNode'); |
116 | 116 | } |
117 | 117 | |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * |
122 | 122 | * @return \DOMNode|null |
123 | 123 | */ |
124 | - public function previous($expression = null, int $index = null){ |
|
124 | + public function previous($expression = null, int $index = null) { |
|
125 | 125 | return $this->_recursivelyFind($expression, $index, 'previousSibling'); |
126 | 126 | } |
127 | 127 | |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * |
132 | 132 | * @return \DOMNode|null |
133 | 133 | */ |
134 | - public function next($expression = null, int $index = null){ |
|
134 | + public function next($expression = null, int $index = null) { |
|
135 | 135 | return $this->_recursivelyFind($expression, $index, 'nextSibling'); |
136 | 136 | } |
137 | 137 | |
@@ -143,11 +143,11 @@ discard block |
||
143 | 143 | public function childElements(int $nodeType = XML_ELEMENT_NODE):NodeList{ |
144 | 144 | $children = new NodeList; |
145 | 145 | |
146 | - if($this->hasChildNodes()){ |
|
146 | + if ($this->hasChildNodes()) { |
|
147 | 147 | |
148 | - foreach($this->childNodes as $child){ |
|
148 | + foreach ($this->childNodes as $child) { |
|
149 | 149 | |
150 | - if($child->nodeType === $nodeType){ |
|
150 | + if ($child->nodeType === $nodeType) { |
|
151 | 151 | $children[] = $child; |
152 | 152 | } |
153 | 153 | |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | /** |
192 | 192 | * @return \DOMNode|null |
193 | 193 | */ |
194 | - public function firstDescendant(){ |
|
194 | + public function firstDescendant() { |
|
195 | 195 | return $this->descendants()->first(); |
196 | 196 | } |
197 | 197 |
@@ -36,15 +36,15 @@ discard block |
||
36 | 36 | * |
37 | 37 | * @throws \Exception |
38 | 38 | */ |
39 | - public function __construct($content = null){ |
|
39 | + public function __construct($content = null) { |
|
40 | 40 | |
41 | - if($content instanceof NodeList || $content instanceof DOMNodeList){ |
|
41 | + if ($content instanceof NodeList || $content instanceof DOMNodeList) { |
|
42 | 42 | $this->nodelist = iterator_to_array($content); |
43 | 43 | } |
44 | - elseif(is_array($content)){ |
|
44 | + elseif (is_array($content)) { |
|
45 | 45 | $this->nodelist = $content; |
46 | 46 | } |
47 | - else{ |
|
47 | + else { |
|
48 | 48 | $this->nodelist = []; |
49 | 49 | } |
50 | 50 | |
@@ -57,9 +57,9 @@ discard block |
||
57 | 57 | */ |
58 | 58 | public function match(DOMNode $node):bool{ |
59 | 59 | |
60 | - foreach($this->nodelist as $element){ |
|
60 | + foreach ($this->nodelist as $element) { |
|
61 | 61 | |
62 | - if($element->isSameNode($node)){ |
|
62 | + if ($element->isSameNode($node)) { |
|
63 | 63 | return true; |
64 | 64 | } |
65 | 65 | |
@@ -80,14 +80,14 @@ discard block |
||
80 | 80 | /** |
81 | 81 | * @return \DOMNode|null |
82 | 82 | */ |
83 | - public function first(){ |
|
83 | + public function first() { |
|
84 | 84 | return $this->nodelist[0] ?? null; |
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | 88 | * @return \DOMNode|null |
89 | 89 | */ |
90 | - public function last(){ |
|
90 | + public function last() { |
|
91 | 91 | return $this->nodelist[$this->count() - 1] ?? null; |
92 | 92 | } |
93 | 93 | |
@@ -157,14 +157,14 @@ discard block |
||
157 | 157 | /** |
158 | 158 | * @return void |
159 | 159 | */ |
160 | - public function next(){ |
|
160 | + public function next() { |
|
161 | 161 | $this->key++; |
162 | 162 | } |
163 | 163 | |
164 | 164 | /** |
165 | 165 | * @return void |
166 | 166 | */ |
167 | - public function rewind(){ |
|
167 | + public function rewind() { |
|
168 | 168 | $this->key = 0; |
169 | 169 | } |
170 | 170 | |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | * |
188 | 188 | * @return \DOMNode|null |
189 | 189 | */ |
190 | - public function offsetGet($offset){ |
|
190 | + public function offsetGet($offset) { |
|
191 | 191 | return $this->item($offset); |
192 | 192 | } |
193 | 193 | |
@@ -197,12 +197,12 @@ discard block |
||
197 | 197 | * |
198 | 198 | * @return void |
199 | 199 | */ |
200 | - public function offsetSet($offset, $value){ |
|
200 | + public function offsetSet($offset, $value) { |
|
201 | 201 | |
202 | - if(is_int($offset)){ |
|
202 | + if (is_int($offset)) { |
|
203 | 203 | $this->nodelist[$offset] = $value; |
204 | 204 | } |
205 | - else{ |
|
205 | + else { |
|
206 | 206 | $this->nodelist[] = $value; |
207 | 207 | } |
208 | 208 | |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | * |
214 | 214 | * @return void |
215 | 215 | */ |
216 | - public function offsetUnset($offset){ |
|
216 | + public function offsetUnset($offset) { |
|
217 | 217 | unset($this->nodelist[$offset]); |
218 | 218 | } |
219 | 219 |
@@ -40,11 +40,9 @@ discard block |
||
40 | 40 | |
41 | 41 | if($content instanceof NodeList || $content instanceof DOMNodeList){ |
42 | 42 | $this->nodelist = iterator_to_array($content); |
43 | - } |
|
44 | - elseif(is_array($content)){ |
|
43 | + } elseif(is_array($content)){ |
|
45 | 44 | $this->nodelist = $content; |
46 | - } |
|
47 | - else{ |
|
45 | + } else{ |
|
48 | 46 | $this->nodelist = []; |
49 | 47 | } |
50 | 48 | |
@@ -201,8 +199,7 @@ discard block |
||
201 | 199 | |
202 | 200 | if(is_int($offset)){ |
203 | 201 | $this->nodelist[$offset] = $value; |
204 | - } |
|
205 | - else{ |
|
202 | + } else{ |
|
206 | 203 | $this->nodelist[] = $value; |
207 | 204 | } |
208 | 205 |