Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| DocumentationSidebar | |
100.00% |
19 / 19 |
|
100.00% |
9 / 9 |
18 | |
100.00% |
1 / 1 |
| generate | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| hasGroups | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getGroups | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getItemsInGroup | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isGroupActive | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| makeGroupTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| canAddRoute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| isPageIndexPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| shouldIndexPageBeActive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Framework\Features\Navigation; |
| 6 | |
| 7 | use Hyde\Hyde; |
| 8 | use Hyde\Facades\Config; |
| 9 | use Hyde\Foundation\Facades\Routes; |
| 10 | use Hyde\Pages\DocumentationPage; |
| 11 | use Hyde\Support\Facades\Render; |
| 12 | use Hyde\Support\Models\Route; |
| 13 | use Illuminate\Support\Collection; |
| 14 | |
| 15 | use function collect; |
| 16 | |
| 17 | class DocumentationSidebar extends BaseNavigationMenu |
| 18 | { |
| 19 | protected function generate(): void |
| 20 | { |
| 21 | Routes::getRoutes(DocumentationPage::class)->each(function (Route $route): void { |
| 22 | if ($this->canAddRoute($route)) { |
| 23 | $this->items->put($route->getRouteKey(), NavItem::fromRoute($route)); |
| 24 | } |
| 25 | }); |
| 26 | |
| 27 | // If there are no pages other than the index page, we add it to the sidebar so that it's not empty |
| 28 | if ($this->items->count() === 0 && DocumentationPage::home() !== null) { |
| 29 | $this->items->push(NavItem::fromRoute(DocumentationPage::home(), group: 'other')); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public function hasGroups(): bool |
| 34 | { |
| 35 | return (count($this->getGroups()) >= 1) && ($this->getGroups() !== ['other']); |
| 36 | } |
| 37 | |
| 38 | /** @return array<string> */ |
| 39 | public function getGroups(): array |
| 40 | { |
| 41 | return $this->items->map(function (NavItem $item): string { |
| 42 | return $item->getGroup(); |
| 43 | })->unique()->toArray(); |
| 44 | } |
| 45 | |
| 46 | /** @return Collection<\Hyde\Framework\Features\Navigation\NavItem> */ |
| 47 | public function getItemsInGroup(?string $group): Collection |
| 48 | { |
| 49 | return $this->items->filter(function (NavItem $item) use ($group): bool { |
| 50 | return ($item->getGroup() === $group) || ($item->getGroup() === Hyde::makeSlug($group)); |
| 51 | })->sortBy('navigation.priority')->values(); |
| 52 | } |
| 53 | |
| 54 | public function isGroupActive(string $group): bool |
| 55 | { |
| 56 | $normalized = Hyde::makeSlug(Render::getPage()->navigationMenuGroup() ?? 'other'); |
| 57 | |
| 58 | return ($normalized === $group) || ($this->isPageIndexPage() && $this->shouldIndexPageBeActive($group)); |
| 59 | } |
| 60 | |
| 61 | public function makeGroupTitle(string $group): string |
| 62 | { |
| 63 | return Config::getNullableString("docs.sidebar_group_labels.$group") ?? Hyde::makeTitle($group); |
| 64 | } |
| 65 | |
| 66 | protected function canAddRoute(Route $route): bool |
| 67 | { |
| 68 | return parent::canAddRoute($route) && ! $route->is(DocumentationPage::homeRouteName()); |
| 69 | } |
| 70 | |
| 71 | private function isPageIndexPage(): bool |
| 72 | { |
| 73 | return Render::getPage()->getRoute()->is(DocumentationPage::homeRouteName()); |
| 74 | } |
| 75 | |
| 76 | private function shouldIndexPageBeActive(string $group): bool |
| 77 | { |
| 78 | return Render::getPage()->navigationMenuGroup() === 'other' && $group === collect($this->getGroups())->first(); |
| 79 | } |
| 80 | } |