ILIAS  release_8 Revision v8.24
ComponentEntries.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22
24use Iterator;
25use Countable;
26use JsonSerializable;
27
33class ComponentEntries extends AbstractEntryPart implements Iterator, Countable, JsonSerializable
34{
38 protected array $id_to_entry_map = array();
39
40 protected string $root_entry_id = 'root';
41
42 public function __construct()
43 {
45 $this->rewind();
46 }
47
53 public function addEntry(ComponentEntry $entry): void
54 {
55 $this->assert()->isNotIndex($entry->getId(), $this->id_to_entry_map);
56 if (count($this) == 0) {
57 $this->setRootEntryId($entry->getId());
58 }
59 $this->id_to_entry_map[$entry->getId()] = $entry;
60 }
61
65 public function addEntries(ComponentEntries $entries): void
66 {
67 foreach ($entries as $entry) {
68 $this->addEntry($entry);
69 }
70 }
71
75 public function addEntriesFromArray(array $entries): void
76 {
77 foreach ($entries as $entry_array) {
78 $this->addEntry(new Crawler\Entry\ComponentEntry($entry_array));
79 }
80 }
81
82 public function setRootEntryId(string $root_entry_id): void
83 {
84 $this->root_entry_id = $root_entry_id;
85 }
86
87 public function getRootEntryId(): string
88 {
90 }
91
92 public function getRootEntry(): ComponentEntry
93 {
94 return $this->getEntryById($this->getRootEntryId());
95 }
96
100 public function getEntryById(string $id = ""): ComponentEntry
101 {
102 if (array_key_exists($id, $this->id_to_entry_map)) {
103 return $this->id_to_entry_map[$id];
104 }
105 throw $this->f->exception(Crawler\Exception\CrawlerException::INVALID_ID, $id);
106 }
107
111 public function getParentsOfEntry(string $id): array
112 {
113 $parent_id = $this->getEntryById($id)->getParent();
114
115 if (!$parent_id) {
116 return array();
117 } else {
118 $parents = $this->getParentsOfEntry($parent_id);
119 array_push($parents, $parent_id);
120 return $parents;
121 }
122 }
123
124 public function isParentOfEntry(string $parent_id, string $entry_id): bool
125 {
126 return in_array($parent_id, $this->getParentsOfEntry($entry_id));
127 }
128
132 public function getParentsOfEntryTitles(string $id): array
133 {
134 $titles = array();
135 foreach ($this->getParentsOfEntry($id) as $parent_id) {
136 $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
137 }
138 return $titles;
139 }
140
144 public function getDescendantsOfEntry(string $id): array
145 {
146 $children = $this->getEntryById($id)->getChildren();
147 foreach ($this->getEntryById($id)->getChildren() as $child) {
148 $children = array_merge($children, $this->getDescendantsOfEntry($child));
149 }
150 return $children;
151 }
152
158 public function getChildrenOfEntry(string $id): array
159 {
160 $children = [];
161 foreach ($this->getEntryById($id)->getChildren() as $child_id) {
162 $children[] = $this->getEntryById($child_id);
163 }
164 return $children;
165 }
166
170 public function getDescendantsOfEntryTitles(string $id): array
171 {
172 $titles = array();
173 foreach ($this->getDescendantsOfEntry($id) as $parent_id) {
174 $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
175 }
176 return $titles;
177 }
178
179 public function expose(): array
180 {
181 return get_object_vars($this);
182 }
183
187 public function valid(): bool
188 {
189 return current($this->id_to_entry_map) !== false;
190 }
191
195 public function key()
196 {
197 return key($this->id_to_entry_map);
198 }
199
203 public function current()
204 {
205 return current($this->id_to_entry_map);
206 }
207
208 public function next(): void
209 {
210 next($this->id_to_entry_map);
211 }
212
213 public function rewind(): void
214 {
215 reset($this->id_to_entry_map);
216 }
217
221 public function count(): int
222 {
223 return count($this->id_to_entry_map);
224 }
225
229 public function jsonSerialize(): array
230 {
231 $serialized = [];
232 foreach ($this->id_to_entry_map as $id => $item) {
233 $serialized[$id] = $item->jsonSerialize();
234 }
235 return $serialized;
236 }
237}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Abstract Entry Part to share some common entry functionality.
Container storing a list of UI Component Entries, can act as Iterator, countable and is serializable.
addEntry(ComponentEntry $entry)
Add and entry, first is always root.
isParentOfEntry(string $parent_id, string $entry_id)
Stores Information of UI Components parsed from YAML, examples and less files.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Crawler.php:21