ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilHtmlDomNodeIterator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25final class ilHtmlDomNodeIterator implements RecursiveIterator
26{
27 private int $position;
28 private readonly DOMNodeList $nodeList;
29
30 public function __construct(DOMNode $el)
31 {
32 $this->position = 0;
33 if ($el instanceof DOMDocument) {
34 $root = $el->documentElement;
35 } elseif ($el instanceof DOMElement) {
36 $root = $el;
37 } else {
38 throw new InvalidArgumentException('Invalid arguments, expected DOMElement or DOMDocument');
39 }
40
41 $this->nodeList = $root->childNodes;
42 }
43
44 public function key(): int
45 {
46 return $this->position;
47 }
48
49 public function next(): void
50 {
51 $this->position++;
52 }
53
54 public function current(): DOMNode
55 {
56 return $this->nodeList->item($this->position);
57 }
58
59 public function valid(): bool
60 {
61 return $this->position < $this->nodeList->length;
62 }
63
64 public function rewind(): void
65 {
66 $this->position = 0;
67 }
68
69 public function hasChildren(): bool
70 {
71 return $this->current()->hasChildNodes();
72 }
73
74 public function getChildren(): self
75 {
76 return new self($this->current());
77 }
78}
Class ilHtmlDomNodeIterator.