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