Complex classes like HtmlPageCrawler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlPageCrawler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class HtmlPageCrawler extends Crawler |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * the (internal) root element name used when importing html fragments |
||
| 19 | * */ |
||
| 20 | const FRAGMENT_ROOT_TAGNAME = '_root'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get an HtmlPageCrawler object from a HTML string, DOMNode, DOMNodeList or HtmlPageCrawler |
||
| 24 | * |
||
| 25 | * This is the equivalent to jQuery's $() function when used for wrapping DOMNodes or creating DOMElements from HTML code. |
||
| 26 | * |
||
| 27 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|array $content |
||
| 28 | * @return HtmlPageCrawler |
||
| 29 | * @api |
||
| 30 | */ |
||
| 31 | 18 | public static function create($content) |
|
| 32 | { |
||
| 33 | 18 | if ($content instanceof HtmlPageCrawler) { |
|
| 34 | 3 | return $content; |
|
| 35 | } else { |
||
| 36 | 18 | return new HtmlPageCrawler($content); |
|
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Adds the specified class(es) to each element in the set of matched elements. |
||
| 42 | * |
||
| 43 | * @param string $name One or more space-separated classes to be added to the class attribute of each matched element. |
||
| 44 | * @return HtmlPageCrawler $this for chaining |
||
| 45 | * @api |
||
| 46 | */ |
||
| 47 | 1 | public function addClass($name) |
|
| 48 | { |
||
| 49 | 1 | foreach ($this as $node) { |
|
| 50 | 1 | if ($node instanceof \DOMElement) { |
|
| 51 | /** @var \DOMElement $node */ |
||
| 52 | 1 | $classes = preg_split('/\s+/s', $node->getAttribute('class')); |
|
| 53 | 1 | $found = false; |
|
| 54 | 1 | $count = count($classes); |
|
| 55 | 1 | for ($i = 0; $i < $count; $i++) { |
|
| 56 | 1 | if ($classes[$i] == $name) { |
|
| 57 | 1 | $found = true; |
|
| 58 | 1 | } |
|
| 59 | 1 | } |
|
| 60 | 1 | if (!$found) { |
|
| 61 | 1 | $classes[] = $name; |
|
| 62 | 1 | $node->setAttribute('class', trim(join(' ', $classes))); |
|
| 63 | 1 | } |
|
| 64 | 1 | } |
|
| 65 | 1 | } |
|
| 66 | 1 | return $this; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Insert content, specified by the parameter, after each element in the set of matched elements. |
||
| 71 | * |
||
| 72 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 73 | * @return HtmlPageCrawler $this for chaining |
||
| 74 | * @api |
||
| 75 | */ |
||
| 76 | 3 | public function after($content) |
|
| 77 | { |
||
| 78 | 3 | $content = self::create($content); |
|
| 79 | 3 | $newnodes = array(); |
|
| 80 | 3 | foreach ($this as $i => $node) { |
|
| 81 | /** @var \DOMNode $node */ |
||
| 82 | 3 | $refnode = $node->nextSibling; |
|
| 83 | 3 | foreach ($content as $newnode) { |
|
| 84 | /** @var \DOMNode $newnode */ |
||
| 85 | 3 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 86 | 3 | if ($refnode === null) { |
|
| 87 | 3 | $node->parentNode->appendChild($newnode); |
|
| 88 | 3 | } else { |
|
| 89 | 1 | $node->parentNode->insertBefore($newnode, $refnode); |
|
| 90 | } |
||
| 91 | 3 | $newnodes[] = $newnode; |
|
| 92 | 3 | } |
|
| 93 | 3 | } |
|
| 94 | 3 | $content->clear(); |
|
| 95 | 3 | $content->add($newnodes); |
|
| 96 | 3 | return $this; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Insert HTML content as child nodes of each element after existing children |
||
| 101 | * |
||
| 102 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment or DOMNode to append |
||
| 103 | * @return HtmlPageCrawler $this for chaining |
||
| 104 | * @api |
||
| 105 | */ |
||
| 106 | 2 | public function append($content) |
|
| 107 | { |
||
| 108 | 2 | $content = self::create($content); |
|
| 109 | 2 | $newnodes = array(); |
|
| 110 | 2 | foreach ($this as $i => $node) { |
|
| 111 | /** @var \DOMNode $node */ |
||
| 112 | 2 | foreach ($content as $newnode) { |
|
| 113 | /** @var \DOMNode $newnode */ |
||
| 114 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 115 | // if ($newnode->ownerDocument !== $node->ownerDocument) { |
||
| 116 | // $newnode = $node->ownerDocument->importNode($newnode, true); |
||
| 117 | // } else { |
||
| 118 | // if ($i > 0) { |
||
| 119 | // $newnode = $newnode->cloneNode(true); |
||
| 120 | // } |
||
| 121 | // } |
||
| 122 | 2 | $node->appendChild($newnode); |
|
| 123 | 2 | $newnodes[] = $newnode; |
|
| 124 | 2 | } |
|
| 125 | 2 | } |
|
| 126 | 2 | $content->clear(); |
|
| 127 | 2 | $content->add($newnodes); |
|
| 128 | 2 | return $this; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Insert every element in the set of matched elements to the end of the target. |
||
| 133 | * |
||
| 134 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 135 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 136 | * @api |
||
| 137 | */ |
||
| 138 | 1 | public function appendTo($element) |
|
| 139 | { |
||
| 140 | 1 | $e = self::create($element); |
|
| 141 | 1 | $newnodes = array(); |
|
| 142 | 1 | foreach ($e as $i => $node) { |
|
| 143 | /** @var \DOMNode $node */ |
||
| 144 | 1 | foreach ($this as $newnode) { |
|
| 145 | /** @var \DOMNode $newnode */ |
||
| 146 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 147 | 1 | $node->appendChild($newnode); |
|
| 148 | 1 | $newnodes[] = $newnode; |
|
| 149 | 1 | } |
|
| 150 | 1 | } |
|
| 151 | 1 | return self::create($newnodes); |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the attribute value of the first node of the list, or sets an attribute on each element |
||
| 156 | * |
||
| 157 | * @see HtmlPageCrawler::getAttribute() |
||
| 158 | * @see HtmlPageCrawler::setAttribute |
||
| 159 | * |
||
| 160 | * @param string $name |
||
| 161 | * @param null|string $value |
||
| 162 | * @return null|string|HtmlPageCrawler |
||
| 163 | * @api |
||
| 164 | */ |
||
| 165 | 2 | public function attr($name, $value = null) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Sets an attribute on each element |
||
| 176 | * |
||
| 177 | * @param string $name |
||
| 178 | * @param string $value |
||
| 179 | * @return HtmlPageCrawler $this for chaining |
||
| 180 | */ |
||
| 181 | 3 | public function setAttribute($name, $value) |
|
| 182 | { |
||
| 183 | 3 | foreach ($this as $node) { |
|
| 184 | 3 | if ($node instanceof \DOMElement) { |
|
| 185 | /** @var \DOMElement $node */ |
||
| 186 | 3 | $node->setAttribute($name, $value); |
|
| 187 | 3 | } |
|
| 188 | 3 | } |
|
| 189 | 3 | return $this; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the attribute value of the first node of the list. |
||
| 194 | * |
||
| 195 | * @param string $name The attribute name |
||
| 196 | * @return string|null The attribute value or null if the attribute does not exist |
||
| 197 | * @throws \InvalidArgumentException When current node is empty |
||
| 198 | * |
||
| 199 | */ |
||
| 200 | 2 | public function getAttribute($name) |
|
| 201 | { |
||
| 202 | 2 | if (!count($this)) { |
|
| 203 | 1 | throw new \InvalidArgumentException('The current node list is empty.'); |
|
| 204 | } |
||
| 205 | 1 | $node = $this->getNode(0); |
|
| 206 | 1 | return $node->hasAttribute($name) ? $node->getAttribute($name) : null; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Insert content, specified by the parameter, before each element in the set of matched elements. |
||
| 211 | * |
||
| 212 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 213 | * @return HtmlPageCrawler $this for chaining |
||
| 214 | * @api |
||
| 215 | */ |
||
| 216 | 2 | public function before($content) |
|
| 217 | { |
||
| 218 | 2 | $content = self::create($content); |
|
| 219 | 2 | $newnodes = array(); |
|
| 220 | 2 | foreach ($this as $i => $node) { |
|
| 221 | /** @var \DOMNode $node */ |
||
| 222 | 2 | foreach ($content as $newnode) { |
|
| 223 | /** @var \DOMNode $newnode */ |
||
| 224 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 225 | 2 | $node->parentNode->insertBefore($newnode, $node); |
|
| 226 | 2 | $newnodes[] = $newnode; |
|
| 227 | 2 | } |
|
| 228 | 2 | } |
|
| 229 | 2 | $content->clear(); |
|
| 230 | 2 | $content->add($newnodes); |
|
| 231 | 2 | return $this; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Create a deep copy of the set of matched elements. |
||
| 236 | * |
||
| 237 | * Equivalent to clone() in jQuery (clone is not a valid PHP function name) |
||
| 238 | * |
||
| 239 | * @return HtmlPageCrawler |
||
| 240 | * @api |
||
| 241 | */ |
||
| 242 | 1 | public function makeClone() |
|
| 246 | |||
| 247 | 1 | public function __clone() |
|
| 248 | { |
||
| 249 | 1 | $newnodes = array(); |
|
| 250 | 1 | foreach ($this as $node) { |
|
| 251 | /** @var \DOMNode $node */ |
||
| 252 | 1 | $newnodes[] = $node->cloneNode(true); |
|
| 253 | 1 | } |
|
| 254 | 1 | $this->clear(); |
|
| 255 | 1 | $this->add($newnodes); |
|
| 256 | 1 | } |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get one CSS style property of the first element or set it for all elements in the list |
||
| 260 | * |
||
| 261 | * Function is here for compatibility with jQuery; it is the same as getStyle() and setStyle() |
||
| 262 | * |
||
| 263 | * @see HtmlPageCrawler::getStyle() |
||
| 264 | * @see HtmlPageCrawler::setStyle() |
||
| 265 | * |
||
| 266 | * @param string $key The name of the style property |
||
| 267 | * @param null|string $value The CSS value to set, or NULL to get the current value |
||
| 268 | * @return HtmlPageCrawler|string If no param is provided, returns the CSS styles of the first element |
||
| 269 | * @api |
||
| 270 | */ |
||
| 271 | 1 | public function css($key, $value = null) |
|
| 272 | { |
||
| 273 | 1 | if (null === $value) { |
|
| 274 | 1 | return $this->getStyle($key); |
|
| 275 | } else { |
||
| 276 | 1 | return $this->setStyle($key, $value); |
|
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * get one CSS style property of the first element |
||
| 282 | * |
||
| 283 | * @param string $key name of the property |
||
| 284 | * @return string|null value of the property |
||
| 285 | */ |
||
| 286 | 1 | public function getStyle($key) |
|
| 287 | { |
||
| 288 | 1 | $styles = Helpers::cssStringToArray($this->getAttribute('style')); |
|
| 289 | 1 | return (isset($styles[$key]) ? $styles[$key] : null); |
|
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * set one CSS style property for all elements in the list |
||
| 294 | * |
||
| 295 | * @param string $key name of the property |
||
| 296 | * @param string $value value of the property |
||
| 297 | * @return HtmlPageCrawler $this for chaining |
||
| 298 | */ |
||
| 299 | 1 | public function setStyle($key, $value) |
|
| 300 | { |
||
| 301 | 1 | foreach ($this as $node) { |
|
| 302 | 1 | if ($node instanceof \DOMElement) { |
|
| 303 | /** @var \DOMElement $node */ |
||
| 304 | 1 | $styles = Helpers::cssStringToArray($node->getAttribute('style')); |
|
| 305 | 1 | if ($value != '') { |
|
| 306 | 1 | $styles[$key] = $value; |
|
| 307 | 1 | } elseif (isset($styles[$key])) { |
|
| 308 | 1 | unset($styles[$key]); |
|
| 309 | 1 | } |
|
| 310 | 1 | $node->setAttribute('style', Helpers::cssArrayToString($styles)); |
|
| 311 | 1 | } |
|
| 312 | 1 | } |
|
| 313 | 1 | return $this; |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Removes all child nodes and text from all nodes in set |
||
| 318 | * |
||
| 319 | * Equivalent to jQuery's empty() function which is not a valid function name in PHP |
||
| 320 | * @return HtmlPageCrawler $this |
||
| 321 | * @api |
||
| 322 | */ |
||
| 323 | 1 | public function makeEmpty() |
|
| 324 | { |
||
| 325 | 1 | foreach ($this as $node) { |
|
| 326 | 1 | $node->nodeValue = ''; |
|
| 327 | 1 | } |
|
| 328 | 1 | return $this; |
|
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Determine whether any of the matched elements are assigned the given class. |
||
| 333 | * |
||
| 334 | * @param string $name |
||
| 335 | * @return bool |
||
| 336 | * @api |
||
| 337 | */ |
||
| 338 | 2 | public function hasClass($name) |
|
| 339 | { |
||
| 340 | 2 | foreach ($this as $node) { |
|
| 341 | 2 | if ($node instanceof \DOMElement && $class = $node->getAttribute('class')) { |
|
| 342 | 2 | $classes = preg_split('/\s+/s', $class); |
|
| 343 | 2 | if (in_array($name, $classes)) { |
|
| 344 | 2 | return true; |
|
| 345 | } |
||
| 346 | 1 | } |
|
| 347 | 2 | } |
|
| 348 | 2 | return false; |
|
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the HTML contents of the first element in the set of matched elements |
||
| 353 | * or set the HTML contents of every matched element. |
||
| 354 | * |
||
| 355 | * Function is here for compatibility with jQuery: When called with a parameter, it is |
||
| 356 | * equivalent to setInnerHtml(), without parameter it is the same as getInnerHtml() |
||
| 357 | * |
||
| 358 | * @see HtmlPageCrawler::setInnerHtml() |
||
| 359 | * @see HtmlPageCrawler::getInnerHtml() |
||
| 360 | * |
||
| 361 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|null $html The HTML content to set, or NULL to get the current content |
||
| 362 | * |
||
| 363 | * @return HtmlPageCrawler|string If no param is provided, returns the HTML content of the first element |
||
| 364 | * @api |
||
| 365 | */ |
||
| 366 | 1 | public function html($html = null) |
|
| 367 | { |
||
| 368 | 1 | if (null === $html) { |
|
| 369 | 1 | return $this->getInnerHtml(); |
|
| 370 | } else { |
||
| 371 | 1 | $this->setInnerHtml($html); |
|
| 372 | 1 | return $this; |
|
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the innerHTML contents of the first element |
||
| 378 | * |
||
| 379 | * @return string HTML code fragment |
||
| 380 | */ |
||
| 381 | 4 | public function getInnerHtml() |
|
| 382 | { |
||
| 383 | 4 | $node = $this->getNode(0); |
|
| 384 | 4 | if ($node instanceof \DOMNode) { |
|
| 385 | 3 | $doc = new \DOMDocument('1.0', 'UTF-8'); |
|
| 386 | 3 | $doc->appendChild($doc->importNode($node, true)); |
|
| 387 | 3 | $html = trim($doc->saveHTML()); |
|
| 388 | 3 | $tag = $node->nodeName; |
|
| 389 | 3 | return preg_replace('@^<' . $tag . '[^>]*>|</' . $tag . '>$@', '', $html); |
|
| 390 | } else { |
||
| 391 | 1 | return ''; |
|
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set the HTML contents of each element |
||
| 397 | * |
||
| 398 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 399 | * @return HtmlPageCrawler $this for chaining |
||
| 400 | */ |
||
| 401 | 3 | public function setInnerHtml($content) |
|
| 402 | { |
||
| 403 | 3 | $content = self::create($content); |
|
| 404 | 3 | foreach ($this as $node) { |
|
| 405 | 3 | $node->nodeValue = ''; |
|
| 406 | 3 | foreach ($content as $newnode) { |
|
| 407 | /** @var \DOMNode $node */ |
||
| 408 | /** @var \DOMNode $newnode */ |
||
| 409 | 3 | $newnode = static::importNewnode($newnode, $node); |
|
| 410 | 3 | $node->appendChild($newnode); |
|
| 411 | 3 | } |
|
| 412 | 3 | } |
|
| 413 | 3 | return $this; |
|
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Insert every element in the set of matched elements after the target. |
||
| 418 | * |
||
| 419 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 420 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 421 | * @api |
||
| 422 | */ |
||
| 423 | 1 | public function insertAfter($element) |
|
| 424 | { |
||
| 425 | 1 | $e = self::create($element); |
|
| 426 | 1 | $newnodes = array(); |
|
| 427 | 1 | foreach ($e as $i => $node) { |
|
| 428 | /** @var \DOMNode $node */ |
||
| 429 | 1 | $refnode = $node->nextSibling; |
|
| 430 | 1 | foreach ($this as $newnode) { |
|
| 431 | /** @var \DOMNode $newnode */ |
||
| 432 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 433 | 1 | if ($refnode === null) { |
|
| 434 | 1 | $node->parentNode->appendChild($newnode); |
|
| 435 | 1 | } else { |
|
| 436 | 1 | $node->parentNode->insertBefore($newnode, $refnode); |
|
| 437 | } |
||
| 438 | 1 | $newnodes[] = $newnode; |
|
| 439 | 1 | } |
|
| 440 | 1 | } |
|
| 441 | 1 | return self::create($newnodes); |
|
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Insert every element in the set of matched elements before the target. |
||
| 446 | * |
||
| 447 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 448 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 449 | * @api |
||
| 450 | */ |
||
| 451 | 1 | public function insertBefore($element) |
|
| 452 | { |
||
| 453 | 1 | $e = self::create($element); |
|
| 454 | 1 | $newnodes = array(); |
|
| 455 | 1 | foreach ($e as $i => $node) { |
|
| 456 | /** @var \DOMNode $node */ |
||
| 457 | 1 | foreach ($this as $newnode) { |
|
| 458 | /** @var \DOMNode $newnode */ |
||
| 459 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 460 | 1 | $node->parentNode->insertBefore($newnode, $node); |
|
| 461 | 1 | $newnodes[] = $newnode; |
|
| 462 | 1 | } |
|
| 463 | 1 | } |
|
| 464 | 1 | return self::create($newnodes); |
|
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
| 469 | * |
||
| 470 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 471 | * @return HtmlPageCrawler $this for chaining |
||
| 472 | * @api |
||
| 473 | */ |
||
| 474 | 2 | public function prepend($content) |
|
| 475 | { |
||
| 476 | 2 | $content = self::create($content); |
|
| 477 | 2 | $newnodes = array(); |
|
| 478 | 2 | foreach ($this as $i => $node) { |
|
| 479 | 2 | $refnode = $node->firstChild; |
|
| 480 | /** @var \DOMNode $node */ |
||
| 481 | 2 | foreach ($content as $newnode) { |
|
| 482 | /** @var \DOMNode $newnode */ |
||
| 483 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 484 | 2 | if ($refnode === null) { |
|
| 485 | 1 | $node->appendChild($newnode); |
|
| 486 | 1 | } else { |
|
| 487 | 2 | $node->insertBefore($newnode, $refnode); |
|
| 488 | } |
||
| 489 | 2 | $newnodes[] = $newnode; |
|
| 490 | 2 | } |
|
| 491 | 2 | } |
|
| 492 | 2 | $content->clear(); |
|
| 493 | 2 | $content->add($newnodes); |
|
| 494 | 2 | return $this; |
|
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Insert every element in the set of matched elements to the beginning of the target. |
||
| 499 | * |
||
| 500 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 501 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
| 502 | * @api |
||
| 503 | */ |
||
| 504 | 1 | public function prependTo($element) |
|
| 505 | { |
||
| 506 | 1 | $e = self::create($element); |
|
| 507 | 1 | $newnodes = array(); |
|
| 508 | 1 | foreach ($e as $i => $node) { |
|
| 509 | 1 | $refnode = $node->firstChild; |
|
| 510 | /** @var \DOMNode $node */ |
||
| 511 | 1 | foreach ($this as $newnode) { |
|
| 512 | /** @var \DOMNode $newnode */ |
||
| 513 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 514 | 1 | if ($refnode === null) { |
|
| 515 | 1 | $node->appendChild($newnode); |
|
| 516 | 1 | } else { |
|
| 517 | 1 | $node->insertBefore($newnode, $refnode); |
|
| 518 | } |
||
| 519 | 1 | $newnodes[] = $newnode; |
|
| 520 | 1 | } |
|
| 521 | 1 | } |
|
| 522 | 1 | return self::create($newnodes); |
|
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Remove the set of matched elements from the DOM. |
||
| 527 | * |
||
| 528 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
| 529 | * but leaves them in the DOM) |
||
| 530 | * |
||
| 531 | * @api |
||
| 532 | */ |
||
| 533 | 2 | public function remove() |
|
| 534 | { |
||
| 535 | 2 | foreach ($this as $node) { |
|
| 536 | /** |
||
| 537 | * @var \DOMNode $node |
||
| 538 | */ |
||
| 539 | 2 | if ($node->parentNode instanceof \DOMElement) { |
|
| 540 | 2 | $node->parentNode->removeChild($node); |
|
| 541 | 2 | } |
|
| 542 | 2 | } |
|
| 543 | 2 | $this->clear(); |
|
| 544 | 2 | } |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Remove an attribute from each element in the set of matched elements. |
||
| 548 | * |
||
| 549 | * Alias for removeAttribute for compatibility with jQuery |
||
| 550 | * |
||
| 551 | * @param string $name |
||
| 552 | * @return HtmlPageCrawler |
||
| 553 | * @api |
||
| 554 | */ |
||
| 555 | 1 | public function removeAttr($name) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Remove an attribute from each element in the set of matched elements. |
||
| 562 | * |
||
| 563 | * @param string $name |
||
| 564 | * @return HtmlPageCrawler |
||
| 565 | */ |
||
| 566 | 1 | public function removeAttribute($name) |
|
| 567 | { |
||
| 568 | 1 | foreach ($this as $node) { |
|
| 569 | 1 | if ($node instanceof \DOMElement) { |
|
| 570 | /** @var \DOMElement $node */ |
||
| 571 | 1 | if ($node->hasAttribute($name)) { |
|
| 572 | 1 | $node->removeAttribute($name); |
|
| 573 | 1 | } |
|
| 574 | 1 | } |
|
| 575 | 1 | } |
|
| 576 | 1 | return $this; |
|
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Remove a class from each element in the list |
||
| 581 | * |
||
| 582 | * @param string $name |
||
| 583 | * @return HtmlPageCrawler $this for chaining |
||
| 584 | * @api |
||
| 585 | */ |
||
| 586 | 2 | public function removeClass($name) |
|
| 587 | { |
||
| 588 | 2 | foreach ($this as $node) { |
|
| 589 | 2 | if ($node instanceof \DOMElement) { |
|
| 590 | /** @var \DOMElement $node */ |
||
| 591 | 2 | $classes = preg_split('/\s+/s', $node->getAttribute('class')); |
|
| 592 | 2 | $count = count($classes); |
|
| 593 | 2 | for ($i = 0; $i < $count; $i++) { |
|
| 594 | 2 | if ($classes[$i] == $name) { |
|
| 595 | 2 | unset($classes[$i]); |
|
| 596 | 2 | } |
|
| 597 | 2 | } |
|
| 598 | 2 | $node->setAttribute('class', trim(join(' ', $classes))); |
|
| 599 | 2 | } |
|
| 600 | 2 | } |
|
| 601 | 2 | return $this; |
|
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Replace each target element with the set of matched elements. |
||
| 606 | * |
||
| 607 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 608 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 609 | * @api |
||
| 610 | */ |
||
| 611 | 2 | public function replaceAll($element) |
|
| 612 | { |
||
| 613 | 2 | $e = self::create($element); |
|
| 614 | 2 | $newnodes = array(); |
|
| 615 | 2 | foreach ($e as $i => $node) { |
|
| 616 | /** @var \DOMNode $node */ |
||
| 617 | 2 | $parent = $node->parentNode; |
|
| 618 | 2 | $refnode = $node->nextSibling; |
|
| 619 | 2 | foreach ($this as $j => $newnode) { |
|
| 620 | /** @var \DOMNode $newnode */ |
||
| 621 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 622 | 2 | if ($j == 0) { |
|
| 623 | 2 | $parent->replaceChild($newnode, $node); |
|
| 624 | 2 | } else { |
|
| 625 | 1 | $parent->insertBefore($newnode, $refnode); |
|
| 626 | } |
||
| 627 | 2 | $newnodes[] = $newnode; |
|
| 628 | 2 | } |
|
| 629 | 2 | } |
|
| 630 | 2 | return self::create($newnodes); |
|
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
| 635 | * |
||
| 636 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 637 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 638 | * @api |
||
| 639 | */ |
||
| 640 | 2 | public function replaceWith($content) |
|
| 641 | { |
||
| 642 | 2 | $content = self::create($content); |
|
| 643 | 2 | $newnodes = array(); |
|
| 644 | 2 | foreach ($this as $i => $node) { |
|
| 645 | /** @var \DOMNode $node */ |
||
| 646 | 2 | $parent = $node->parentNode; |
|
| 647 | 2 | $refnode = $node->nextSibling; |
|
| 648 | 2 | foreach ($content as $j => $newnode) { |
|
| 649 | /** @var \DOMNode $newnode */ |
||
| 650 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
| 651 | 2 | if ($j == 0) { |
|
| 652 | 2 | $parent->replaceChild($newnode, $node); |
|
| 653 | 2 | } else { |
|
| 654 | 1 | $parent->insertBefore($newnode, $refnode); |
|
| 655 | } |
||
| 656 | 2 | $newnodes[] = $newnode; |
|
| 657 | 2 | } |
|
| 658 | 2 | } |
|
| 659 | 2 | $content->clear(); |
|
| 660 | 2 | $content->add($newnodes); |
|
| 661 | 2 | return $this; |
|
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Get the combined text contents of each element in the set of matched elements, including their descendants, |
||
| 666 | * or set the text contents of the matched elements. |
||
| 667 | * |
||
| 668 | * ATTENTION: Contrary to the parent Crawler class, which returns the text from the first element only, |
||
| 669 | * this functions returns the combined text of all elements (as jQuery does). If this is not what you need you |
||
| 670 | * must call ->first() before calling ->text(), e.g. |
||
| 671 | * |
||
| 672 | * in Symfony\DOMCrawler\Crawler: $c->filter('p')->text() returns the text of the first paragraph only |
||
| 673 | * in HtmlPageCrawler you need to call: $c->filter('p')->first()->text() |
||
| 674 | * |
||
| 675 | * @param null|string $text |
||
| 676 | * @return string|HtmlPageCrawler |
||
| 677 | * @api |
||
| 678 | */ |
||
| 679 | 1 | public function text($text = null) |
|
| 696 | |||
| 697 | |||
| 698 | /** |
||
| 699 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
| 700 | * |
||
| 701 | * @param string $classname One or more classnames separated by spaces |
||
| 702 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 703 | * @api |
||
| 704 | */ |
||
| 705 | 1 | public function toggleClass($classname) |
|
| 706 | { |
||
| 707 | 1 | $classes = explode(' ', $classname); |
|
| 708 | 1 | foreach ($this as $i => $node) { |
|
| 709 | 1 | $c = self::create($node); |
|
| 710 | /** @var \DOMNode $node */ |
||
| 711 | 1 | foreach ($classes as $class) { |
|
| 712 | 1 | if ($c->hasClass($class)) { |
|
| 713 | 1 | $c->removeClass($class); |
|
| 714 | 1 | } else { |
|
| 715 | 1 | $c->addClass($class); |
|
| 716 | } |
||
| 717 | 1 | } |
|
| 718 | 1 | } |
|
| 719 | 1 | return $this; |
|
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
| 724 | * |
||
| 725 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 726 | * @api |
||
| 727 | */ |
||
| 728 | 1 | public function unwrap() |
|
| 729 | { |
||
| 730 | 1 | $parents = array(); |
|
| 731 | 1 | foreach($this as $i => $node) { |
|
| 732 | 1 | $parents[] = $node->parentNode; |
|
| 733 | 1 | } |
|
| 734 | |||
| 735 | 1 | self::create($parents)->unwrapInner(); |
|
| 736 | 1 | return $this; |
|
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Remove the matched elements, but promote the children to take their place. |
||
| 741 | * |
||
| 742 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 743 | * @api |
||
| 744 | */ |
||
| 745 | 2 | public function unwrapInner() |
|
| 746 | { |
||
| 747 | 2 | foreach($this as $i => $node) { |
|
| 748 | 2 | if (!$node->parentNode instanceof \DOMElement) { |
|
| 749 | 1 | throw new \InvalidArgumentException('DOMElement does not have a parent DOMElement node.'); |
|
| 750 | } |
||
| 751 | |||
| 752 | /** @var \DOMNode[] $children */ |
||
| 753 | 2 | $children = iterator_to_array($node->childNodes); |
|
| 754 | 2 | foreach ($children as $child) { |
|
| 755 | 1 | $node->parentNode->insertBefore($child, $node); |
|
| 756 | 2 | } |
|
| 757 | |||
| 758 | 2 | $node->parentNode->removeChild($node); |
|
| 759 | 2 | } |
|
| 760 | 2 | } |
|
| 761 | |||
| 762 | |||
| 763 | /** |
||
| 764 | * Wrap an HTML structure around each element in the set of matched elements |
||
| 765 | * |
||
| 766 | * The HTML structure must contain only one root node, e.g.: |
||
| 767 | * Works: <div><div></div></div> |
||
| 768 | * Does not work: <div></div><div></div> |
||
| 769 | * |
||
| 770 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
| 771 | * @return HtmlPageCrawler $this for chaining |
||
| 772 | * @api |
||
| 773 | */ |
||
| 774 | 1 | public function wrap($wrappingElement) |
|
| 775 | { |
||
| 776 | 1 | $content = self::create($wrappingElement); |
|
| 777 | 1 | $newnodes = array(); |
|
| 778 | 1 | foreach ($this as $i => $node) { |
|
| 779 | /** @var \DOMNode $node */ |
||
| 780 | 1 | $newnode = $content->getNode(0); |
|
| 781 | /** @var \DOMNode $newnode */ |
||
| 782 | // $newnode = static::importNewnode($newnode, $node, $i); |
||
| 783 | 1 | if ($newnode->ownerDocument !== $node->ownerDocument) { |
|
| 784 | 1 | $newnode = $node->ownerDocument->importNode($newnode, true); |
|
| 785 | 1 | } else { |
|
| 786 | if ($i > 0) { |
||
| 787 | $newnode = $newnode->cloneNode(true); |
||
| 788 | } |
||
| 789 | } |
||
| 790 | 1 | $oldnode = $node->parentNode->replaceChild($newnode, $node); |
|
| 791 | 1 | while ($newnode->hasChildNodes()) { |
|
| 792 | 1 | $elementFound = false; |
|
| 793 | 1 | foreach ($newnode->childNodes as $child) { |
|
| 794 | 1 | if ($child instanceof \DOMElement) { |
|
| 795 | 1 | $newnode = $child; |
|
| 796 | 1 | $elementFound = true; |
|
| 797 | 1 | break; |
|
| 798 | } |
||
| 799 | 1 | } |
|
| 800 | 1 | if (!$elementFound) { |
|
| 801 | 1 | break; |
|
| 802 | } |
||
| 803 | 1 | } |
|
| 804 | 1 | $newnode->appendChild($oldnode); |
|
| 805 | 1 | $newnodes[] = $newnode; |
|
| 806 | 1 | } |
|
| 807 | 1 | $content->clear(); |
|
| 808 | 1 | $content->add($newnodes); |
|
| 809 | 1 | return $this; |
|
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
| 814 | * |
||
| 815 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 816 | * @throws \LogicException |
||
| 817 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 818 | * @api |
||
| 819 | */ |
||
| 820 | 1 | public function wrapAll($content) |
|
| 821 | { |
||
| 822 | 1 | $content = self::create($content); |
|
| 823 | 1 | $parent = $this->getNode(0)->parentNode; |
|
| 824 | 1 | foreach ($this as $i => $node) { |
|
| 825 | /** @var \DOMNode $node */ |
||
| 826 | 1 | if ($node->parentNode !== $parent) { |
|
| 827 | throw new \LogicException('Nodes to be wrapped with wrapAll() must all have the same parent'); |
||
| 828 | } |
||
| 829 | 1 | } |
|
| 830 | |||
| 831 | 1 | $newnode = $content->getNode(0); |
|
| 832 | /** @var \DOMNode $newnode */ |
||
| 833 | 1 | $newnode = static::importNewnode($newnode, $parent); |
|
| 834 | |||
| 835 | 1 | $newnode = $parent->insertBefore($newnode,$this->getNode(0)); |
|
| 836 | 1 | $content->clear(); |
|
| 837 | 1 | $content->add($newnode); |
|
| 838 | |||
| 839 | 1 | while ($newnode->hasChildNodes()) { |
|
| 840 | 1 | $elementFound = false; |
|
| 841 | 1 | foreach ($newnode->childNodes as $child) { |
|
| 842 | 1 | if ($child instanceof \DOMElement) { |
|
| 843 | 1 | $newnode = $child; |
|
| 844 | 1 | $elementFound = true; |
|
| 845 | 1 | break; |
|
| 846 | } |
||
| 847 | 1 | } |
|
| 848 | 1 | if (!$elementFound) { |
|
| 849 | break; |
||
| 850 | } |
||
| 851 | 1 | } |
|
| 852 | 1 | foreach ($this as $i => $node) { |
|
| 853 | /** @var \DOMNode $node */ |
||
| 854 | 1 | $newnode->appendChild($node); |
|
| 855 | 1 | } |
|
| 856 | 1 | return $this; |
|
| 857 | } |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
| 861 | * |
||
| 862 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 863 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 864 | * @api |
||
| 865 | */ |
||
| 866 | 1 | public function wrapInner($content) |
|
| 867 | { |
||
| 868 | 1 | foreach ($this as $i => $node) { |
|
| 869 | /** @var \DOMNode $node */ |
||
| 870 | 1 | self::create($node->childNodes)->wrapAll($content); |
|
| 871 | 1 | } |
|
| 872 | 1 | return $this; |
|
| 873 | } |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Get the HTML code fragment of all elements and their contents. |
||
| 877 | * |
||
| 878 | * If the first node contains a complete HTML document return only |
||
| 879 | * the full code of this document. |
||
| 880 | * |
||
| 881 | * @return string HTML code (fragment) |
||
| 882 | * @api |
||
| 883 | */ |
||
| 884 | 8 | public function saveHTML() |
|
| 885 | { |
||
| 886 | 8 | if ($this->isHtmlDocument()) { |
|
| 887 | 1 | return $this->getDOMDocument()->saveHTML(); |
|
| 888 | } else { |
||
| 889 | 8 | $doc = new \DOMDocument('1.0', 'UTF-8'); |
|
| 890 | 8 | $root = $doc->appendChild($doc->createElement('_root')); |
|
| 891 | 8 | foreach ($this as $node) { |
|
| 892 | 8 | $root->appendChild($doc->importNode($node, true)); |
|
| 893 | 8 | } |
|
| 894 | 8 | $html = trim($doc->saveHTML()); |
|
| 895 | 8 | return preg_replace('@^<'.self::FRAGMENT_ROOT_TAGNAME.'[^>]*>|</'.self::FRAGMENT_ROOT_TAGNAME.'>$@', '', $html); |
|
| 896 | } |
||
| 897 | } |
||
| 898 | |||
| 899 | 4 | public function __toString() |
|
| 900 | { |
||
| 901 | 4 | return $this->saveHTML(); |
|
| 902 | } |
||
| 903 | |||
| 904 | /** |
||
| 905 | * checks whether the first node contains a complete html document |
||
| 906 | * (as opposed to a document fragment) |
||
| 907 | * |
||
| 908 | * @return boolean |
||
| 909 | */ |
||
| 910 | 8 | public function isHtmlDocument() |
|
| 911 | { |
||
| 912 | 8 | $node = $this->getNode(0); |
|
| 913 | if ($node instanceof \DOMElement |
||
| 914 | 8 | && $node->ownerDocument instanceof \DOMDocument |
|
| 915 | 8 | && $node->ownerDocument->documentElement === $node |
|
| 916 | 8 | && $node->nodeName == 'html' |
|
| 917 | 8 | ) { |
|
| 918 | 1 | return true; |
|
| 919 | } else { |
||
| 920 | 8 | return false; |
|
| 921 | } |
||
| 922 | } |
||
| 923 | |||
| 924 | /** |
||
| 925 | * get ownerDocument of the first element |
||
| 926 | * |
||
| 927 | * @return \DOMDocument|null |
||
| 928 | */ |
||
| 929 | 1 | public function getDOMDocument() |
|
| 930 | { |
||
| 931 | 1 | $node = $this->getNode(0); |
|
| 932 | 1 | $r = null; |
|
| 933 | if ($node instanceof \DOMElement |
||
| 934 | 1 | && $node->ownerDocument instanceof \DOMDocument |
|
| 935 | 1 | ) { |
|
| 936 | 1 | $r = $node->ownerDocument; |
|
| 937 | 1 | } |
|
| 938 | 1 | return $r; |
|
| 939 | } |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Filters the list of nodes with a CSS selector. |
||
| 943 | * |
||
| 944 | * @param string $selector |
||
| 945 | * @return HtmlPageCrawler |
||
| 946 | */ |
||
| 947 | 8 | public function filter($selector) |
|
| 948 | { |
||
| 949 | 8 | return parent::filter($selector); |
|
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Filters the list of nodes with an XPath expression. |
||
| 954 | * |
||
| 955 | * @param string $xpath An XPath expression |
||
| 956 | * |
||
| 957 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
| 958 | * |
||
| 959 | * @api |
||
| 960 | */ |
||
| 961 | 2 | public function filterXPath($xpath) |
|
| 962 | { |
||
| 963 | 2 | return parent::filterXPath($xpath); |
|
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
| 968 | * |
||
| 969 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
| 970 | * |
||
| 971 | * |
||
| 972 | * @param string $content A string to parse as HTML/XML |
||
| 973 | * @param null|string $type The content type of the string |
||
| 974 | * |
||
| 975 | * @return null|void |
||
| 976 | */ |
||
| 977 | 17 | public function addContent($content, $type = null) |
|
| 978 | { |
||
| 979 | 17 | if (empty($type)) { |
|
| 980 | 17 | $type = 'text/html;charset=UTF-8'; |
|
| 981 | 17 | } |
|
| 982 | 17 | if (substr($type, 0, 9) == 'text/html' && !preg_match('/<html\b[^>]*>/i', $content)) { |
|
| 983 | // string contains no <html> Tag => no complete document but an HTML fragment! |
||
| 984 | 16 | $this->addHtmlFragment($content); |
|
| 985 | 16 | } else { |
|
| 986 | 2 | parent::addContent($content, $type); |
|
| 987 | } |
||
| 988 | 17 | } |
|
| 989 | |||
| 990 | 15 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
| 991 | { |
||
| 992 | 15 | $d = new \DOMDocument('1.0', $charset); |
|
| 993 | 15 | $root = $d->appendChild($d->createElement(self::FRAGMENT_ROOT_TAGNAME)); |
|
| 994 | 15 | $bodynode = Helpers::getBodyNodeFromHtmlFragment($content, $charset); |
|
| 995 | 15 | foreach ($bodynode->childNodes as $child) { |
|
| 996 | 15 | $inode = $root->appendChild($d->importNode($child, true)); |
|
| 997 | 15 | if ($inode) { |
|
| 998 | 15 | $this->addNode($inode); |
|
| 999 | 15 | } |
|
| 1000 | 15 | } |
|
| 1001 | 15 | } |
|
| 1002 | |||
| 1003 | /** |
||
| 1004 | * returns the first node |
||
| 1005 | * deprecated, use getNode(0) instead |
||
| 1006 | * |
||
| 1007 | * @return \DOMNode|null |
||
| 1008 | * @deprecated |
||
| 1009 | * @see Crawler::getNode |
||
| 1010 | */ |
||
| 1011 | 1 | public function getFirstNode() |
|
| 1012 | { |
||
| 1013 | 1 | return $this->getNode(0); |
|
| 1014 | } |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * @param int $position |
||
| 1018 | * |
||
| 1019 | * overridden from Crawler because it is not public in Symfony 2.3 |
||
| 1020 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1021 | * |
||
| 1022 | * @return \DOMElement|null |
||
| 1023 | */ |
||
| 1024 | 14 | public function getNode($position) |
|
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Returns the node name of the first node of the list. |
||
| 1031 | * |
||
| 1032 | * in Crawler (parent), this function will be available starting with 2.6.0, |
||
| 1033 | * therefore this method be removed from here as soon as we don't need to keep compatibility |
||
| 1034 | * with Symfony < 2.6 |
||
| 1035 | * |
||
| 1036 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1037 | * |
||
| 1038 | * @return string The node name |
||
| 1039 | * |
||
| 1040 | * @throws \InvalidArgumentException When current node is empty |
||
| 1041 | */ |
||
| 1042 | 1 | public function nodeName() |
|
| 1043 | { |
||
| 1044 | 1 | if (!count($this)) { |
|
| 1045 | throw new \InvalidArgumentException('The current node list is empty.'); |
||
| 1046 | } |
||
| 1047 | 1 | return $this->getNode(0)->nodeName; |
|
| 1048 | } |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Adds a node to the current list of nodes. |
||
| 1052 | * |
||
| 1053 | * This method uses the appropriate specialized add*() method based |
||
| 1054 | * on the type of the argument. |
||
| 1055 | * |
||
| 1056 | * Overwritten from parent to allow Crawler to be added |
||
| 1057 | * |
||
| 1058 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
| 1059 | * |
||
| 1060 | * @api |
||
| 1061 | */ |
||
| 1062 | 29 | public function add($node) |
|
| 1063 | { |
||
| 1064 | 29 | if ($node instanceof Crawler) { |
|
| 1065 | 1 | foreach ($node as $childnode) { |
|
| 1066 | 1 | $this->addNode($childnode); |
|
| 1067 | 1 | } |
|
| 1068 | 1 | } else { |
|
| 1069 | 29 | parent::add($node); |
|
| 1070 | } |
||
| 1071 | 29 | } |
|
| 1072 | |||
| 1073 | /** |
||
| 1074 | * @param \DOMNode $newnode |
||
| 1075 | * @param \DOMNode $referencenode |
||
| 1076 | * @param int $clone |
||
| 1077 | * @return \DOMNode |
||
| 1078 | */ |
||
| 1079 | 6 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
| 1092 | * |
||
| 1093 | * @return bool |
||
| 1094 | */ |
||
| 1095 | 1 | public function isDisconnected() |
|
| 1100 | |||
| 1101 | 1 | public function __get($name) |
|
| 1102 | { |
||
| 1103 | switch ($name) { |
||
| 1104 | 1 | case 'count': |
|
| 1105 | 1 | case 'length': |
|
| 1106 | 1 | return count($this); |
|
| 1107 | } |
||
| 1108 | 1 | throw new \Exception('No such property ' . $name); |
|
| 1109 | } |
||
| 1110 | } |
||
| 1111 |
Really long classes often contain too much logic and violate the single responsibility principle.
We suggest to take a look at the “Code” section for options on how to refactor this code.