ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  {
31  parent::__construct();
32  $this->rewind();
33  }
34 
41  public function addEntry(ComponentEntry $entry)
42  {
43  $this->assert()->isNotIndex($entry->getId(), $this->id_to_entry_map);
44  if (count($this) == 0) {
45  $this->setRootEntryId($entry->getId());
46  }
47  $this->id_to_entry_map[$entry->getId()] = $entry;
48  }
49 
54  public function addEntries(ComponentEntries $entries)
55  {
56  foreach ($entries as $entry) {
57  $this->addEntry($entry);
58  }
59  }
60 
64  public function setRootEntryId($root_entry_id)
65  {
66  $this->root_entry_id = $root_entry_id;
67  }
68 
72  public function getRootEntryId()
73  {
74  return $this->root_entry_id;
75  }
76 
80  public function getRootEntry()
81  {
82  return $this->getEntryById($this->getRootEntryId());
83  }
84 
90  public function getEntryById($id = "")
91  {
92  if (array_key_exists($id, $this->id_to_entry_map)) {
93  return $this->id_to_entry_map[$id];
94  }
95  throw $this->f->exception(Crawler\Exception\CrawlerException::INVALID_ID, $id);
96  }
97 
102  public function getParentsOfEntry($id)
103  {
104  $parent_id = $this->getEntryById($id)->getParent();
105 
106  if (!$parent_id) {
107  return array();
108  } else {
109  $parents = $this->getParentsOfEntry($parent_id);
110  array_push($parents, $parent_id);
111  return $parents;
112  }
113  }
114 
119  public function getParentsOfEntryTitles($id)
120  {
121  $titles = array();
122  foreach ($this->getParentsOfEntry($id) as $parent_id) {
123  $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
124  }
125  return $titles;
126  }
127 
132  public function getDescendantsOfEntry($id)
133  {
134  $children = $this->getEntryById($id)->getChildren();
135  foreach ($this->getEntryById($id)->getChildren() as $child) {
136  $children = array_merge($children, $this->getDescendantsOfEntry($child));
137  }
138  return $children;
139  }
140 
146  {
147  $titles = array();
148  foreach ($this->getDescendantsOfEntry($id) as $parent_id) {
149  $titles[$parent_id] = $this->getEntryById($parent_id)->getTitle();
150  }
151  return $titles;
152  }
153 
154  public function expose()
155  {
156  return get_object_vars($this);
157  }
158 
164  public function valid()
165  {
166  return current($this->id_to_entry_map) !== false;
167  }
168 
172  public function key()
173  {
174  return key($this->id_to_entry_map);
175  }
176 
180  public function current()
181  {
182  return current($this->id_to_entry_map);
183  }
184 
185  public function next()
186  {
187  next($this->id_to_entry_map);
188  }
189  public function rewind()
190  {
191  reset($this->id_to_entry_map);
192  }
193 
197  public function count()
198  {
199  return count($this->id_to_entry_map);
200  }
201 
207  public function jsonSerialize()
208  {
209  return $this->id_to_entry_map;
210  }
211 }
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...
if(!array_key_exists('StateId', $_REQUEST)) $id
Stores Information of UI Components parsed from YAML, examples and less files.
addEntry(ComponentEntry $entry)
Add and entry, first is always root.