ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
Map.php
Go to the documentation of this file.
2
3use ArrayObject;
4use Closure;
12
18class Map implements Filterable, Walkable
19{
20
24 protected $raw;
28 protected $filters = [];
32 private $filtered;
36 private $factory;
37
42 {
43 $this->raw = new ArrayObject();
44 $this->factory = $factory;
45 }
46
47 private function getSorter() : Closure
48 {
49 return function (isItem $item_one, isItem $item_two) : int {
50 return $item_one->getPosition() - $item_two->getPosition();
51 };
52 }
53
57 public function add(isItem $item) : void
58 {
59 $serialize = $item->getProviderIdentification()->serialize();
60 if (0 < strlen($serialize)) {
61 $this->raw[$serialize] = $item;
62 }
63 }
64
68 public function addMultiple(isItem ...$items) : void
69 {
70 foreach ($items as $item) {
71 $this->add($item);
72 }
73 }
74
79 public function getSingleItemFromRaw(IdentificationInterface $identification) : isItem
80 {
81 $item = $this->raw->offsetGet($identification->serialize());
82
83 return $item ?? $this->getLostItem($identification);
84 }
85
90 public function getSingleItemFromFilter(IdentificationInterface $identification) : isItem
91 {
92 $this->applyFilters();
93 $item = $this->filtered->offsetGet($identification->serialize());
94
95 return $item ?? $this->getLostItem($identification);
96 }
97
101 public function remove(IdentificationInterface $identification) : void
102 {
103 $this->raw->offsetUnset($identification->serialize());
104 }
105
110 public function existsInFilter(IdentificationInterface $identification) : bool
111 {
112 $this->applyFilters();
113
114 return $this->filtered->offsetExists($identification->serialize());
115 }
116
120 public function has() : bool
121 {
122 return $this->raw->count() > 0;
123 }
124
125 private function applyFilters() : void
126 {
127 if ($this->filtered === null) {
128 $this->filtered = new ArrayObject($this->raw->getArrayCopy());
129 }
130 if (count($this->filters) > 0) {
131 $filter_copy = [];
132 if ($this->filtered === null) {
133 $filter_copy = $this->raw->getArrayCopy();
134 }
135 if ($this->filtered instanceof ArrayObject) {
136 $filter_copy = $this->filtered->getArrayCopy();
137 }
138 foreach ($this->filters as $filter) {
139 $filter_copy = array_filter($filter_copy, $filter);
140 }
141 $this->filtered->exchangeArray($filter_copy);
142 $this->filters = [];
143 }
144 }
145
149 public function getAllFromFilter() : \Generator
150 {
151 $this->applyFilters();
152
153 yield from $this->filtered;
154 }
155
159 public function walk(Closure $c) : void
160 {
161 $this->applyFilters();
162 $to_walk = (array) $this->filtered->getArrayCopy();
163 array_walk($to_walk, $c);
164 $this->filtered = new ArrayObject($to_walk);
165 }
166
170 public function filter(Closure $c) : void
171 {
172 $this->filters[] = $c;
173 }
174
175 public function sort() : void
176 {
177 $this->applyFilters();
178
179 $this->filtered->uasort($this->getSorter());
180
181 $replace_children_sorted = function (isItem &$item) {
182 if ($item instanceof isParent) {
183 $children = $item->getChildren();
184 uasort($children, $this->getSorter());
185 $item = $item->withChildren($children);
186 }
187 };
188 $this->walk($replace_children_sorted);
189 }
190
191 private function getLostItem(IdentificationInterface $identification) : Lost
192 {
193 return $this->factory->custom(Lost::class, new NullIdentification($identification))
194 ->withAlwaysAvailable(true)
195 ->withVisibilityCallable(
196 function () : bool {
197 return false;
198 }
199 )->withTitle('Lost');
200 }
201}
An exception for terminatinating execution or to throw for unit testing.
getSingleItemFromRaw(IdentificationInterface $identification)
Definition: Map.php:79
__construct(MainMenuItemFactory $factory)
Tree constructor.
Definition: Map.php:41
getSingleItemFromFilter(IdentificationInterface $identification)
Definition: Map.php:90
existsInFilter(IdentificationInterface $identification)
Definition: Map.php:110
getLostItem(IdentificationInterface $identification)
Definition: Map.php:191
getPosition()
Return the default position for installation, this will be overridden by the configuration later.