ILIAS  release_7 Revision v7.30-3-g800a261c036
MetaBarMainCollector.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
21
27use Generator;
28
34{
38 private $providers;
42 private $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() ?? function ($v, $k) : bool {
65 return !empty($v);
66 }, $this->getVisibleFilter() === null ? ARRAY_FILTER_USE_BOTH : 0);
67 }
68
69 public function prepareItemsForUIRepresentation() : void
70 {
71 // noting to do here
72 }
73
74 public function cleanupItemsForUIRepresentation() : void
75 {
76 // noting to do here
77 }
78
79 public function sortItemsForUIRepresentation() : void
80 {
81 $this->sortItems($this->items);
82 array_walk($this->items, $this->getChildSorter());
83 }
84
88 public function getItemsForUIRepresentation() : Generator
89 {
90 yield from $this->items;
91 }
92
93 public function hasItems() : bool
94 {
95 return count($this->items) > 0;
96 }
97
98 public function hasVisibleItems() : bool
99 {
100 return $this->hasItems();
101 }
102
103 private function sortItems(&$items)
104 {
105 usort($items, $this->getItemSorter());
106 }
107
108 private function getItemSorter() : callable
109 {
110 return static function (isItem $a, isItem $b) : int {
111 return $a->getPosition() - $b->getPosition();
112 };
113 }
114
115 private function getChildSorter() : callable
116 {
117 return function (isItem &$item) : void {
118 if ($item instanceof isParent) {
119 $children = $item->getChildren();
120 $this->sortItems($children);
121 $item = $item->withChildren($children);
122 }
123 };
124 }
125
126 protected function getVisibleFilter() : callable
127 {
128 return static function (isItem $item) : bool {
129 return ($item->isAvailable() && $item->isVisible());
130 };
131 }
132}
An exception for terminatinating execution or to throw for unit testing.
__construct(array $providers)
MetaBarMainCollector constructor.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples