$items sichtbare Einträge, sortiert */ public function __construct(private readonly Collection $items, private readonly string $currentPath) {} /** * @param Collection $items */ public static function forPath(Collection $items, string $path): self { return new self($items, '/'.trim($path, '/')); } /** * Hauptnavigation mit einer Ebene Untermenü. Untereinträge eines ausgeblendeten Eintrags sind ebenfalls verborgen. * * @return list}> */ public function header(): array { $topLevel = $this->items->where('placement', 'header')->whereNull('parent_id'); $entries = []; foreach ($topLevel as $item) { $children = []; foreach ($this->items->where('parent_id', $item->id) as $child) { $href = $child->href(); if ($href !== null) { $children[] = [ 'id' => $child->id, 'label' => $child->label, 'href' => $href, 'current' => $this->isCurrent($child), ]; } } $href = $item->href(); if ($href === null && $children === []) { continue; } $current = $href !== null && $this->isCurrent($item); $entries[] = [ 'id' => $item->id, 'label' => $item->label, 'href' => $href, 'current' => $current, 'activeTrail' => $current || in_array(true, array_column($children, 'current'), true), 'children' => $children, ]; } return $entries; } /** * Spalten des Fußbereichs in fester Reihenfolge; Gruppen ohne Einträge entfallen. * * @return list}> */ public function footerGroups(): array { $groups = []; foreach (NavigationItem::FOOTER_GROUPS as $placement => $heading) { $links = []; foreach ($this->items->where('placement', $placement) as $item) { $href = $item->href(); if ($href !== null) { $links[] = ['label' => $item->label, 'href' => $href, 'current' => $this->isCurrent($item)]; } } if ($links !== []) { $groups[] = ['key' => $placement, 'heading' => $heading, 'links' => $links]; } } return $groups; } private function isCurrent(NavigationItem $item): bool { return $item->targetPath() === $this->currentPath; } }