ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
MetaBarMainCollector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 use Generator;
31 
37 {
41  private array $items = [];
42  private array $raw_items = [];
43  private readonly Map $map;
44 
45  private bool $default_topics = false;
46 
47  public function __construct(
48  private readonly array $providers
49  ) {
50  $this->map = new Map();
51  }
52 
53  private function getProvidersFromList(): \Iterator
54  {
55  yield from $this->providers;
56  }
57 
58 
59  public function collectStructure(): void
60  {
61 
62  foreach ($this->getProvidersFromList() as $provider) {
63  $this->map->addMultiple(...$provider->getMetaBarItems());
64  }
65  }
66 
67  public function filterItemsByVisibilty(bool $async_only = false): void
68  {
69  $this->map->filter($this->getVisibleFilter());
70  }
71 
72  public function getRawItems(): array
73  {
74  return iterator_to_array($this->map->getAllFromRaw());
75  }
76 
77  public function prepareItemsForUIRepresentation(): void
78  {
79  if ($this->default_topics) {
80  $add_default_topic = static function (isItem &$item) use (&$add_default_topic): isItem {
81  if ($item instanceof isDecorateable) {
82  $item = $item->withTopics(new Topic($item->getProviderIdentification()->getInternalIdentifier()));
83  }
84  if ($item instanceof isParent) {
85  foreach ($item->getChildren() as $child) {
86  $child = $add_default_topic($child);
87  }
88  }
89 
90  return $item;
91  };
92 
93  $this->map->walk($add_default_topic);
94  }
95  }
96 
97  public function cleanupItemsForUIRepresentation(): void
98  {
99  // noting to do here
100  }
101 
102  public function sortItemsForUIRepresentation(): void
103  {
104  $this->map->sort();
105  }
106 
108  {
109  foreach ($this->map->getAllFromFilter() as $item) {
110  yield $item;
111  }
112  }
113 
114  public function hasItems(): bool
115  {
116  return $this->map->has();
117  }
118 
119  public function hasVisibleItems(): bool
120  {
121  if (!$this->hasItems()) {
122  return false;
123  }
124  foreach ($this->getItemsForUIRepresentation() as $item) {
125  return $item instanceof isItem;
126  }
127  return false;
128  }
129 
130 
131 
132  private function getItemSorter(): callable
133  {
134  return static fn(isItem $a, isItem $b): int => $a->getPosition() - $b->getPosition();
135  }
136 
137  private function getChildSorter(): callable
138  {
139  return function (isItem &$item): void {
140  if ($item instanceof isParent) {
141  $children = $item->getChildren();
142  $this->sortItems($children);
143  $item = $item->withChildren($children);
144  }
145  };
146  }
147 
148  protected function getVisibleFilter(): callable
149  {
150  return static fn(isItem $item): bool => $item->isAvailable() && $item->isVisible();
151  }
152 }
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