ILIAS  release_7 Revision v7.30-3-g800a261c036
Map.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
21
22use ArrayObject;
23use Closure;
30use Iterator;
31
37class Map implements Filterable, Walkable
38{
42 private $raw;
46 private $filters = [];
50 private $filtered;
54 private $factory;
55
60 {
61 $this->raw = new ArrayObject();
62 $this->factory = $factory;
63 }
64
65 private function getSorter() : Closure
66 {
67 return function (isItem $item_one, isItem $item_two) : int {
68 return $item_one->getPosition() - $item_two->getPosition();
69 };
70 }
71
75 public function add(isItem $item) : void
76 {
77 $serialize = $item->getProviderIdentification()->serialize();
78 if (0 < strlen($serialize)) {
79 $this->raw[$serialize] = $item;
80 }
81 }
82
86 public function addMultiple(isItem ...$items) : void
87 {
88 foreach ($items as $item) {
89 $this->add($item);
90 }
91 }
92
97 public function getSingleItemFromRaw(IdentificationInterface $identification) : isItem
98 {
99 if ($this->raw->offsetExists($identification->serialize())) {
100 $item = $this->raw->offsetGet($identification->serialize());
101
102 return $item ?? $this->getLostItem($identification);
103 }
104 return $this->getLostItem($identification);
105 }
106
111 public function getSingleItemFromFilter(IdentificationInterface $identification) : isItem
112 {
113 $this->applyFilters();
114
115 if ($this->filtered->offsetExists($identification->serialize())) {
116 $item = $this->filtered->offsetGet($identification->serialize());
117 }
118
119 return $item ?? $this->getLostItem($identification);
120 }
121
125 public function remove(IdentificationInterface $identification) : void
126 {
127 $this->raw->offsetUnset($identification->serialize());
128 }
129
134 public function existsInFilter(IdentificationInterface $identification) : bool
135 {
136 $this->applyFilters();
137
138 return $this->filtered->offsetExists($identification->serialize());
139 }
140
141 public function has() : bool
142 {
143 return $this->raw->count() > 0;
144 }
145
146
147 private function applyFilters() : void
148 {
149 if (!isset($this->filtered)) {
150 $this->filtered = new ArrayObject($this->raw->getArrayCopy());
151 }
152 if (count($this->filters) > 0) {
153 if (!isset($this->filtered)) {
154 $filter_copy = $this->raw->getArrayCopy();
155 } else {
156 $filter_copy = $this->filtered->getArrayCopy();
157 }
158 foreach ($this->filters as $filter) {
159 $filter_copy = array_filter($filter_copy, $filter ?? function ($v, $k) : bool {
160 return !empty($v);
161 }, $filter === null ? ARRAY_FILTER_USE_BOTH : 0);
162 }
163 $this->filtered->exchangeArray($filter_copy);
164 $this->filters = [];
165 }
166 }
167
171 public function getAllFromRaw() : \Generator
172 {
173 yield from $this->raw;
174 }
175
179 public function getAllFromFilter() : \Generator
180 {
181 $this->applyFilters();
182
183 yield from $this->filtered;
184 }
185
189 public function walk(Closure $c) : void
190 {
191 $this->applyFilters();
192 $to_walk = (array) $this->filtered->getArrayCopy();
193 array_walk($to_walk, $c);
194 $this->filtered = new ArrayObject($to_walk);
195 }
196
200 public function filter(Closure $c) : void
201 {
202 $this->filters[] = $c;
203 }
204
205 public function sort() : void
206 {
207 $this->applyFilters();
208
209 $this->filtered->uasort($this->getSorter());
210
211 $replace_children_sorted = function (isItem &$item) {
212 if ($item instanceof isParent) {
213 $children = $item->getChildren();
214 uasort($children, $this->getSorter());
215 $item = $item->withChildren($children);
216 }
217 };
218 $this->walk($replace_children_sorted);
219 }
220
221 private function getLostItem(IdentificationInterface $identification) : Lost
222 {
223 return $this->factory->custom(Lost::class, new NullIdentification($identification))
224 ->withAlwaysAvailable(true)
225 ->withVisibilityCallable(
226 function () : bool {
227 return false;
228 }
229 )->withTitle('Lost');
230 }
231}
An exception for terminatinating execution or to throw for unit testing.
getSingleItemFromRaw(IdentificationInterface $identification)
Definition: Map.php:97
__construct(MainMenuItemFactory $factory)
Tree constructor.
Definition: Map.php:59
getSingleItemFromFilter(IdentificationInterface $identification)
Definition: Map.php:111
existsInFilter(IdentificationInterface $identification)
Definition: Map.php:134
getLostItem(IdentificationInterface $identification)
Definition: Map.php:221
Class MainMenuItemFactory This factory provides you all available types for MainMenu GlobalScreen Ite...
$c
Definition: cli.php:37
getPosition()
Return the default position for installation, this will be overridden by the configuration later.