ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ComponentEntries.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use Iterator;
25use Countable;
26use JsonSerializable;
27
34class ComponentEntries extends AbstractEntryPart implements Iterator, Countable, JsonSerializable
35{
37 protected array $id_to_entry_map = [];
38
39 protected string $root_entry_id = 'root';
40
41 public function __construct()
42 {
44 $this->rewind();
45 }
46
52 public function addEntry(ComponentEntry $entry): void
53 {
54 $this->assert()->isNotIndex($entry->getId(), $this->id_to_entry_map);
55 if (count($this) == 0) {
56 $this->setRootEntryId($entry->getId());
57 }
58 $this->id_to_entry_map[$entry->getId()] = $entry;
59 }
60
64 public function addEntries(ComponentEntries $entries): void
65 {
66 foreach ($entries as $entry) {
67 $this->addEntry($entry);
68 }
69 }
70
74 public function addEntriesFromArray(array $entries): void
75 {
76 foreach ($entries as $entry_array) {
77 $this->addEntry(new Crawler\Entry\ComponentEntry($entry_array));
78 }
79 }
80
81 public function setRootEntryId(string $root_entry_id): void
82 {
83 $this->root_entry_id = $root_entry_id;
84 }
85
86 public function getRootEntryId(): string
87 {
89 }
90
91 public function getRootEntry(): ComponentEntry
92 {
93 return $this->getEntryById($this->getRootEntryId());
94 }
95
99 public function getEntryById(string $id = ""): ComponentEntry
100 {
101 if (array_key_exists($id, $this->id_to_entry_map)) {
102 return $this->id_to_entry_map[$id];
103 }
104 throw $this->f->exception(Crawler\Exception\CrawlerException::INVALID_ID, $id);
105 }
106
110 public function getParentsOfEntry(string $id): array
111 {
112 $parent_id = $this->getEntryById($id)->getParent();
113
114 if (!$parent_id) {
115 return array();
116 } else {
117 $parents = $this->getParentsOfEntry($parent_id);
118 array_push($parents, $parent_id);
119 return $parents;
120 }
121 }
122
123 public function isParentOfEntry(string $parent_id, string $entry_id): bool
124 {
125 return in_array($parent_id, $this->getParentsOfEntry($entry_id));
126 }
127
131 public function getParentsOfEntryTitles(string $id): array
132 {
133 $titles = array();
134 foreach ($this->getParentsOfEntry($id) as $parent_id) {
135 $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
136 }
137 return $titles;
138 }
139
143 public function getDescendantsOfEntry(string $id): array
144 {
145 $children = $this->getEntryById($id)->getChildren();
146 foreach ($this->getEntryById($id)->getChildren() as $child) {
147 $children = array_merge($children, $this->getDescendantsOfEntry($child));
148 }
149 return $children;
150 }
151
157 public function getChildrenOfEntry(string $id): array
158 {
159 $children = [];
160 foreach ($this->getEntryById($id)->getChildren() as $child_id) {
161 $children[] = $this->getEntryById($child_id);
162 }
163 return $children;
164 }
165
169 public function getDescendantsOfEntryTitles(string $id): array
170 {
171 $titles = array();
172 foreach ($this->getDescendantsOfEntry($id) as $parent_id) {
173 $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
174 }
175 return $titles;
176 }
177
178 public function expose(): array
179 {
180 return get_object_vars($this);
181 }
182
186 public function valid(): bool
187 {
188 return current($this->id_to_entry_map) !== false;
189 }
190
191 public function key(): string
192 {
193 return key($this->id_to_entry_map);
194 }
195
196 public function current(): ComponentEntry
197 {
198 return current($this->id_to_entry_map);
199 }
200
201 public function next(): void
202 {
203 next($this->id_to_entry_map);
204 }
205
206 public function rewind(): void
207 {
208 reset($this->id_to_entry_map);
209 }
210
214 public function count(): int
215 {
216 return count($this->id_to_entry_map);
217 }
218
222 public function jsonSerialize(): array
223 {
224 $serialized = [];
225 foreach ($this->id_to_entry_map as $id => $item) {
226 $serialized[$id] = $item->jsonSerialize();
227 }
228 return $serialized;
229 }
230}
$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