ILIAS  release_8 Revision v8.24
Map.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
21
22use ArrayObject;
23use Closure;
30use Iterator;
31
37class Map implements Filterable, Walkable
38{
39 private ArrayObject $raw;
43 private array $filters = [];
44 private ArrayObject $filtered;
46
51 {
52 $this->raw = new ArrayObject();
53 $this->factory = $factory;
54 }
55
56 private function getSorter(): Closure
57 {
58 return function (isItem $item_one, isItem $item_two): int {
59 return $item_one->getPosition() - $item_two->getPosition();
60 };
61 }
62
66 public function add(isItem $item): void
67 {
68 $serialize = $item->getProviderIdentification()->serialize();
69 if (0 < strlen($serialize)) {
70 $this->raw[$serialize] = $item;
71 }
72 }
73
77 public function addMultiple(isItem ...$items): void
78 {
79 foreach ($items as $item) {
80 $this->add($item);
81 }
82 }
83
88 public function getSingleItemFromRaw(IdentificationInterface $identification): isItem
89 {
90 if ($this->raw->offsetExists($identification->serialize())) {
91 $item = $this->raw->offsetGet($identification->serialize());
92
93 return $item ?? $this->getLostItem($identification);
94 }
95 return $this->getLostItem($identification);
96 }
97
103 {
104 $this->applyFilters();
105
106 if ($this->filtered->offsetExists($identification->serialize())) {
107 $item = $this->filtered->offsetGet($identification->serialize());
108 }
109
110 return $item ?? $this->getLostItem($identification);
111 }
112
116 public function remove(IdentificationInterface $identification): void
117 {
118 $this->raw->offsetUnset($identification->serialize());
119 }
120
125 public function existsInFilter(IdentificationInterface $identification): bool
126 {
127 $this->applyFilters();
128
129 return $this->filtered->offsetExists($identification->serialize());
130 }
131
132 public function has(): bool
133 {
134 return $this->raw->count() > 0;
135 }
136
137
138 private function applyFilters(): void
139 {
140 if (!isset($this->filtered)) {
141 $this->filtered = new ArrayObject($this->raw->getArrayCopy());
142 }
143 if (count($this->filters) > 0) {
144 if (!isset($this->filtered)) {
145 $filter_copy = $this->raw->getArrayCopy();
146 } else {
147 $filter_copy = $this->filtered->getArrayCopy();
148 }
149 foreach ($this->filters as $filter) {
150 $filter_copy = array_filter($filter_copy, $filter);
151 }
152 $this->filtered->exchangeArray($filter_copy);
153 $this->filters = [];
154 }
155 }
156
160 public function getAllFromRaw(): \Generator
161 {
162 yield from $this->raw;
163 }
164
168 public function getAllFromFilter(): \Generator
169 {
170 $this->applyFilters();
171
172 yield from $this->filtered;
173 }
174
178 public function walk(Closure $c): void
179 {
180 $this->applyFilters();
181 $to_walk = (array) $this->filtered->getArrayCopy();
182 array_walk($to_walk, $c);
183 $this->filtered = new ArrayObject($to_walk);
184 }
185
189 public function filter(Closure $c): void
190 {
191 $this->filters[] = $c;
192 }
193
194 public function sort(): void
195 {
196 $this->applyFilters();
197
198 $this->filtered->uasort($this->getSorter());
199
200 $replace_children_sorted = function (isItem &$item) {
201 if ($item instanceof isParent) {
202 $children = $item->getChildren();
203 uasort($children, $this->getSorter());
204 $item = $item->withChildren($children);
205 }
206 };
207 $this->walk($replace_children_sorted);
208 }
209
210 private function getLostItem(IdentificationInterface $identification): Lost
211 {
212 return $this->factory->custom(Lost::class, new NullIdentification($identification))
213 ->withAlwaysAvailable(true)
214 ->withVisibilityCallable(
215 function (): bool {
216 return false;
217 }
218 )->withTitle('Lost');
219 }
220}
getSingleItemFromRaw(IdentificationInterface $identification)
Definition: Map.php:88
__construct(MainMenuItemFactory $factory)
Tree constructor.
Definition: Map.php:50
getSingleItemFromFilter(IdentificationInterface $identification)
Definition: Map.php:102
existsInFilter(IdentificationInterface $identification)
Definition: Map.php:125
getLostItem(IdentificationInterface $identification)
Definition: Map.php:210
Class MainMenuItemFactory This factory provides you all available types for MainMenu GlobalScreen Ite...
$c
Definition: cli.php:38
getPosition()
Return the default position for installation, this will be overridden by the configuration later.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Filterable.php:20