593160f66e922f2476639d21dfe6a8592f9e41e7
5 use wcf\util\ClassUtil
;
8 * Default implementation of `IObjectTreeNode`.
10 * @author Matthias Schmidt
11 * @copyright 2001-2019 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 protected $children = [];
24 * current iterator key
33 protected $parentNode;
36 * Adds the given node as child node and sets the child node's parent node to this node.
38 * @param IObjectTreeNode $child added child node
39 * @throws \InvalidArgumentException if given object is no (deocrated) instance of this class
41 public function addChild(IObjectTreeNode
$child)
43 if (!($child instanceof $this) && !ClassUtil
::isDecoratedInstanceOf($child, static::class)) {
44 throw new \
InvalidArgumentException("Child has to be a (decorated) instance of '" . static::class . "', but instance of '" . \
get_class($child) . "' given.");
47 $child->setParentNode($this);
49 $this->children
[] = $child;
53 * Returns the number of child nodes.
55 public function count(): int
57 return \
count($this->children
);
61 * Return the currently iterated child node.
63 public function current(): static
65 return $this->children
[$this->index
];
69 * Returns an iterator for the currently iterated child node by returning the node itself.
71 public function getChildren(): static
73 return $this->children
[$this->index
];
77 * Returns the depth of the node within the tree.
79 * The minimum depth is `1`.
81 public function getDepth(): int
86 while ($element->parentNode
->parentNode
!== null) {
88 $element = $element->parentNode
;
95 * Returns the number of open parent nodes.
97 public function getOpenParentNodes(): int
102 while ($element->parentNode
->parentNode
!== null && $element->isLastSibling()) {
104 $element = $element->parentNode
;
111 * Retruns the parent node of this node.
113 public function getParentNode(): ?self
115 return $this->parentNode
;
119 * Returns `true` if the node as any children and return `false` otherwise.
121 public function hasChildren(): bool
123 return !empty($this->children
);
127 * Return the key of the currently iterated child node.
129 public function key(): int
135 * Returns `true` if this node is the last sibling and `false` otherwise.
137 public function isLastSibling(): bool
139 foreach ($this->parentNode
as $key => $child) {
140 if ($child === $this) {
141 return $key === \
count($this->parentNode
) - 1;
145 throw new \
LogicException("Unreachable");
149 * Moves the iteration forward to next child node.
151 public function next(): void
157 * Rewind the iteration to the first child node.
159 public function rewind(): void
165 * Sets the parent node of this node.
167 * @throws \InvalidArgumentException if given object is no (deocrated) instance of this class
169 public function setParentNode(IObjectTreeNode
$parentNode)
171 if (!($parentNode instanceof $this) && !ClassUtil
::isDecoratedInstanceOf($parentNode, static::class)) {
172 throw new \
InvalidArgumentException("Parent has to be a (decorated) instance of '" . static::class . "', but instance of '" . \
get_class($parentNode) . "' given.");
175 $this->parentNode
= $parentNode;
179 * Returns `true` if current iteration position is valid and `false` otherwise.
181 public function valid(): bool
183 return isset($this->children
[$this->index
]);