ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
AbstractMap.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ArrayObject;
24 use Closure;
33 
38 abstract class AbstractMap implements Filterable, Walkable
39 {
40  protected ArrayObject $raw;
44  protected array $filters = [];
46 
47  public function __construct()
48  {
49  $this->raw = new ArrayObject();
50  }
51 
52  protected function getTitleSorter(): Closure
53  {
54  return static fn(isGlobalScreenItem $item_one, isGlobalScreenItem $item_two): int => 0;
55  }
56 
57  protected function getPositionSorter(): Closure
58  {
59  return static fn(isGlobalScreenItem $item_one, isGlobalScreenItem $item_two): int => 0;
60  }
61 
62  public function add(isGlobalScreenItem $item): void
63  {
64  $serialize = $item->getProviderIdentification()->serialize();
65  if (0 < strlen($serialize)) {
66  $this->raw[$serialize] = $item;
67  }
68  }
69 
70  public function addMultiple(isGlobalScreenItem ...$items): void
71  {
72  foreach ($items as $item) {
73  $this->add($item);
74  }
75  }
76 
77  public function getSingleItemFromRaw(IdentificationInterface $identification): ?isGlobalScreenItem
78  {
79  if ($this->raw->offsetExists($identification->serialize())) {
80  $item = $this->raw->offsetGet($identification->serialize());
81 
82  return $item ?? $this->getLostItem($identification);
83  }
84  return $this->getLostItem($identification);
85  }
86 
87  public function getSingleItemFromFilter(IdentificationInterface $identification): ?isGlobalScreenItem
88  {
89  $this->applyFilters();
90 
91  if ($this->filtered->offsetExists($identification->serialize())) {
92  $item = $this->filtered->offsetGet($identification->serialize());
93  }
94 
95  return $item ?? $this->getLostItem($identification);
96  }
97 
98  public function remove(IdentificationInterface $identification): void
99  {
100  $this->raw->offsetUnset($identification->serialize());
101  }
102 
103  public function existsInFilter(IdentificationInterface $identification): bool
104  {
105  $this->applyFilters();
106 
107  return $this->filtered->offsetExists($identification->serialize());
108  }
109 
110  public function has(): bool
111  {
112  return $this->raw->count() > 0;
113  }
114 
115  protected function applyFilters(): void
116  {
117  if (!isset($this->filtered)) {
118  $this->filtered = new ArrayObject($this->raw->getArrayCopy());
119  }
120  if ($this->filters !== []) {
121  $filter_copy = isset($this->filtered) ? $this->filtered->getArrayCopy() : $this->raw->getArrayCopy();
122  foreach ($this->filters as $filter) {
123  $filter_copy = array_filter($filter_copy, $filter);
124  }
125  $this->filtered->exchangeArray($filter_copy);
126  $this->filters = [];
127  }
128  }
129 
133  public function getAllFromRaw(): \Generator
134  {
135  yield from $this->raw;
136  }
137 
141  public function getAllFromFilter(): \Generator
142  {
143  $this->applyFilters();
144 
145  yield from $this->filtered;
146  }
147 
148  public function walk(Closure $c): void
149  {
150  $this->applyFilters();
151  $to_walk = (array) $this->filtered->getArrayCopy();
152  array_walk($to_walk, $c);
153  $this->filtered = new ArrayObject($to_walk);
154  }
155 
156  public function filter(Closure $c): void
157  {
158  $this->filters[] = $c;
159  }
160 
161  public function sort(): void
162  {
163  $this->applyFilters();
164 
165  $this->filtered->uasort($this->getTitleSorter());
166  $this->filtered->uasort($this->getPositionSorter());
167  }
168 
169 
170 
171  protected function getLostItem(IdentificationInterface $identification): ?isGlobalScreenItem
172  {
173  return null;
174  }
175 }
$c
Definition: deliver.php:9
existsInFilter(IdentificationInterface $identification)
addMultiple(isGlobalScreenItem ... $items)
Definition: AbstractMap.php:70
getLostItem(IdentificationInterface $identification)
getSingleItemFromRaw(IdentificationInterface $identification)
Definition: AbstractMap.php:77
getSingleItemFromFilter(IdentificationInterface $identification)
Definition: AbstractMap.php:87