Commit | Line | Data |
---|---|---|
0bdb3cdf | 1 | <?php |
a9229942 | 2 | |
0bdb3cdf | 3 | namespace wcf\data\page; |
a9229942 | 4 | |
2b7a2f53 | 5 | use wcf\data\DatabaseObjectDecorator; |
0bdb3cdf MW |
6 | |
7 | /** | |
8 | * Represents a page node element. | |
a9229942 TD |
9 | * |
10 | * @author Marcel Werk | |
11 | * @copyright 2001-2019 WoltLab GmbH | |
12 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
13 | * @package WoltLabSuite\Core\Data\Page | |
14 | * @since 3.0 | |
15 | * | |
16 | * @method Page getDecoratedObject() | |
17 | * @mixin Page | |
0bdb3cdf | 18 | */ |
a9229942 TD |
19 | class PageNode extends DatabaseObjectDecorator implements \Countable, \RecursiveIterator |
20 | { | |
21 | /** | |
22 | * parent node | |
23 | * @var PageNode | |
24 | */ | |
25 | protected $parentNode; | |
26 | ||
27 | /** | |
28 | * children of this node | |
29 | * @var PageNode[] | |
30 | */ | |
31 | protected $children = []; | |
32 | ||
33 | /** | |
34 | * node depth | |
35 | * @var int | |
36 | */ | |
37 | protected $depth = 0; | |
38 | ||
39 | /** | |
40 | * iterator position | |
41 | * @var int | |
42 | */ | |
43 | private $position = 0; | |
44 | ||
45 | /** | |
46 | * @inheritDoc | |
47 | */ | |
48 | protected static $baseClass = Page::class; | |
49 | ||
50 | /** | |
51 | * Creates a new PageNode object. | |
52 | * | |
53 | * @param PageNode $parentNode | |
54 | * @param Page $page | |
55 | * @param int $depth | |
56 | */ | |
57 | public function __construct($parentNode = null, ?Page $page = null, $depth = 0) | |
58 | { | |
59 | if ($page === null) { | |
60 | $page = new Page(null, []); | |
61 | } | |
62 | parent::__construct($page); | |
63 | ||
64 | $this->parentNode = $parentNode; | |
65 | $this->depth = $depth; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Sets the children of this node. | |
70 | * | |
71 | * @param PageNode[] $children | |
72 | */ | |
73 | public function setChildren(array $children) | |
74 | { | |
75 | $this->children = $children; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Returns the parent node | |
80 | * | |
81 | * @return PageNode | |
82 | */ | |
83 | public function getParentNode() | |
84 | { | |
85 | return $this->parentNode; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Returns the number of children. | |
90 | * | |
91 | * @return int | |
92 | */ | |
93 | public function count() | |
94 | { | |
95 | return \count($this->children); | |
96 | } | |
97 | ||
98 | /** | |
99 | * @inheritDoc | |
100 | */ | |
101 | public function rewind() | |
102 | { | |
103 | $this->position = 0; | |
104 | } | |
105 | ||
106 | /** | |
107 | * @inheritDoc | |
108 | */ | |
109 | public function valid() | |
110 | { | |
111 | return isset($this->children[$this->position]); | |
112 | } | |
113 | ||
114 | /** | |
115 | * @inheritDoc | |
116 | */ | |
117 | public function next() | |
118 | { | |
119 | $this->position++; | |
120 | } | |
121 | ||
122 | /** | |
123 | * @inheritDoc | |
124 | */ | |
125 | public function current() | |
126 | { | |
127 | return $this->children[$this->position]; | |
128 | } | |
129 | ||
130 | /** | |
131 | * @inheritDoc | |
132 | */ | |
133 | public function key() | |
134 | { | |
135 | return $this->position; | |
136 | } | |
137 | ||
138 | /** | |
139 | * @inheritDoc | |
140 | */ | |
141 | public function getChildren() | |
142 | { | |
143 | return $this->children[$this->position]; | |
144 | } | |
145 | ||
146 | /** | |
147 | * @inheritDoc | |
148 | */ | |
149 | public function hasChildren() | |
150 | { | |
151 | return \count($this->children) > 0; | |
152 | } | |
153 | ||
154 | /** | |
155 | * Returns node depth. | |
156 | * | |
157 | * @return int | |
158 | */ | |
159 | public function getDepth() | |
160 | { | |
161 | return $this->depth; | |
162 | } | |
0bdb3cdf | 163 | } |