ILIAS  release_8 Revision v8.24
MetaBarMainCollector.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
21
27use Generator;
28
34{
38 private array $providers;
42 private array $items = [];
43
48 public function __construct(array $providers)
49 {
50 $this->providers = $providers;
51 }
52
53 public function collectStructure(): void
54 {
55 $items_to_merge = [];
56 foreach ($this->providers as $provider) {
57 $items_to_merge[] = $provider->getMetaBarItems();
58 }
59 $this->items = array_merge([], ...$items_to_merge);
60 }
61
62 public function filterItemsByVisibilty(bool $async_only = false): void
63 {
64 $this->items = array_filter($this->items, $this->getVisibleFilter());
65 }
66
67 public function prepareItemsForUIRepresentation(): void
68 {
69 // noting to do here
70 }
71
72 public function cleanupItemsForUIRepresentation(): void
73 {
74 // noting to do here
75 }
76
77 public function sortItemsForUIRepresentation(): void
78 {
79 $this->sortItems($this->items);
80 array_walk($this->items, $this->getChildSorter());
81 }
82
86 public function getItemsForUIRepresentation(): Generator
87 {
88 yield from $this->items;
89 }
90
91 public function hasItems(): bool
92 {
93 return count($this->items) > 0;
94 }
95
96 public function hasVisibleItems(): bool
97 {
98 return $this->hasItems();
99 }
100
101 private function sortItems(&$items)
102 {
103 usort($items, $this->getItemSorter());
104 }
105
106 private function getItemSorter(): callable
107 {
108 return static function (isItem $a, isItem $b): int {
109 return $a->getPosition() - $b->getPosition();
110 };
111 }
112
113 private function getChildSorter(): callable
114 {
115 return function (isItem &$item): void {
116 if ($item instanceof isParent) {
117 $children = $item->getChildren();
118 $this->sortItems($children);
119 $item = $item->withChildren($children);
120 }
121 };
122 }
123
124 protected function getVisibleFilter(): callable
125 {
126 return static function (isItem $item): bool {
127 return ($item->isAvailable() && $item->isVisible());
128 };
129 }
130}
__construct(array $providers)
MetaBarMainCollector constructor.
$provider
Definition: ltitoken.php:83
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples