ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ComponentEntries.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2016 Timon Amstutz <timon.amstutz@ilub.unibe.ch> Extended GPL, see docs/LICENSE */
4 
6 
8 
14 class ComponentEntries extends AbstractEntryPart implements \Iterator, \Countable, \JsonSerializable
15 {
19  protected $root_entry_id = 'root';
20 
24  protected $id_to_entry_map = array();
25 
29  public function __construct()
30  {
32  $this->rewind();
33  }
34 
35  public static function createFromArray(array $entries_array)
36  {
37  $entries = new self();
38  $entries->addEntriesFromArray($entries_array);
39  return $entries;
40  }
41 
48  public function addEntry(ComponentEntry $entry)
49  {
50  $this->assert()->isNotIndex($entry->getId(), $this->id_to_entry_map);
51  if (count($this) == 0) {
52  $this->setRootEntryId($entry->getId());
53  }
54  $this->id_to_entry_map[$entry->getId()] = $entry;
55  }
56 
61  public function addEntries(ComponentEntries $entries)
62  {
63  foreach ($entries as $entry) {
64  $this->addEntry($entry);
65  }
66  }
67 
72  public function addEntriesFromArray(array $entries)
73  {
74  foreach ($entries as $entry_array) {
75  $this->addEntry(new Crawler\Entry\ComponentEntry($entry_array));
76  }
77  }
78 
82  public function setRootEntryId($root_entry_id)
83  {
84  $this->root_entry_id = $root_entry_id;
85  }
86 
90  public function getRootEntryId()
91  {
92  return $this->root_entry_id;
93  }
94 
98  public function getRootEntry()
99  {
100  return $this->getEntryById($this->getRootEntryId());
101  }
102 
108  public function getEntryById($id = "")
109  {
110  if (array_key_exists($id, $this->id_to_entry_map)) {
111  return $this->id_to_entry_map[$id];
112  }
113  throw $this->f->exception(Crawler\Exception\CrawlerException::INVALID_ID, $id);
114  }
115 
120  public function getParentsOfEntry($id)
121  {
122  $parent_id = $this->getEntryById($id)->getParent();
123 
124  if (!$parent_id) {
125  return array();
126  } else {
127  $parents = $this->getParentsOfEntry($parent_id);
128  array_push($parents, $parent_id);
129  return $parents;
130  }
131  }
132 
137  public function getParentsOfEntryTitles($id)
138  {
139  $titles = array();
140  foreach ($this->getParentsOfEntry($id) as $parent_id) {
141  $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
142  }
143  return $titles;
144  }
145 
150  public function getDescendantsOfEntry($id)
151  {
152  $children = $this->getEntryById($id)->getChildren();
153  foreach ($this->getEntryById($id)->getChildren() as $child) {
154  $children = array_merge($children, $this->getDescendantsOfEntry($child));
155  }
156  return $children;
157  }
158 
163  public function getDescendantsOfEntryTitles($id)
164  {
165  $titles = array();
166  foreach ($this->getDescendantsOfEntry($id) as $parent_id) {
167  $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
168  }
169  return $titles;
170  }
171 
172  public function expose()
173  {
174  return get_object_vars($this);
175  }
176 
182  public function valid()
183  {
184  return current($this->id_to_entry_map) !== false;
185  }
186 
190  public function key()
191  {
192  return key($this->id_to_entry_map);
193  }
194 
198  public function current()
199  {
200  return current($this->id_to_entry_map);
201  }
202 
203  public function next()
204  {
205  next($this->id_to_entry_map);
206  }
207  public function rewind()
208  {
209  reset($this->id_to_entry_map);
210  }
211 
215  public function count()
216  {
217  return count($this->id_to_entry_map);
218  }
219 
225  public function jsonSerialize()
226  {
227  $serialized = [];
228  foreach ($this->id_to_entry_map as $id => $item) {
229  $serialized[$id] = $item->jsonSerialize();
230  }
231  return $serialized;
232  }
233 }
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...
Stores Information of UI Components parsed from YAML, examples and less files.
addEntry(ComponentEntry $entry)
Add and entry, first is always root.
__construct(Container $dic, ilPlugin $plugin)