ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
MetaBarMainCollector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 use Generator;
30 
36 {
40  private array $items = [];
41  private array $raw_items = [];
42  private readonly Map $map;
43 
44  private bool $default_topics = false;
45 
46  public function __construct(
47  private readonly array $providers
48  ) {
49  $this->map = new Map();
50  }
51 
52  private function getProvidersFromList(): \Iterator
53  {
54  yield from $this->providers;
55  }
56 
57 
58  public function collectStructure(): void
59  {
60 
61  foreach ($this->getProvidersFromList() as $provider) {
62  $this->map->addMultiple(...$provider->getMetaBarItems());
63  }
64  }
65 
66  public function filterItemsByVisibilty(bool $async_only = false): void
67  {
68  $this->map->filter($this->getVisibleFilter());
69  }
70 
71  public function getRawItems(): array
72  {
73  return iterator_to_array($this->map->getAllFromRaw());
74  }
75 
76  public function prepareItemsForUIRepresentation(): void
77  {
78  if ($this->default_topics) {
79  $add_default_topic = static function (isItem &$item) use (&$add_default_topic): isItem {
80  if ($item instanceof isDecorateable) {
81  $item = $item->withTopics(new Topic($item->getProviderIdentification()->getInternalIdentifier()));
82  }
83  if ($item instanceof isParent) {
84  foreach ($item->getChildren() as $child) {
85  $child = $add_default_topic($child);
86  }
87  }
88 
89  return $item;
90  };
91 
92  $this->map->walk($add_default_topic);
93  }
94  }
95 
96  public function cleanupItemsForUIRepresentation(): void
97  {
98  // noting to do here
99  }
100 
101  public function sortItemsForUIRepresentation(): void
102  {
103  $this->map->sort();
104  }
105 
107  {
108  foreach ($this->map->getAllFromFilter() as $item) {
109  yield $item;
110  }
111  }
112 
113  public function hasItems(): bool
114  {
115  return $this->map->has();
116  }
117 
118  public function hasVisibleItems(): bool
119  {
120  if (!$this->hasItems()) {
121  return false;
122  }
123  foreach ($this->getItemsForUIRepresentation() as $item) {
124  return $item instanceof isItem;
125  }
126  return false;
127  }
128 
129 
130 
131  private function getItemSorter(): callable
132  {
133  return static fn(isItem $a, isItem $b): int => $a->getPosition() - $b->getPosition();
134  }
135 
136  private function getChildSorter(): callable
137  {
138  return function (isItem &$item): void {
139  if ($item instanceof isParent) {
140  $children = $item->getChildren();
141  $this->sortItems($children);
142  $item = $item->withChildren($children);
143  }
144  };
145  }
146 
147  protected function getVisibleFilter(): callable
148  {
149  return static fn(isItem $item): bool => $item->isAvailable() && $item->isVisible();
150  }
151 }
This is just a class that marks a string as a help topic.
Definition: Topic.php:26
getPosition()
Return the default position for installation, this will be overridden by the configuration later...
$provider
Definition: ltitoken.php:80
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples