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 | use Illuminate\Support\Str; |
15 | |
16 | use function collect; |
17 | |
18 | class DocumentationSidebar extends BaseNavigationMenu |
19 | { |
20 | protected function generate(): void |
21 | { |
22 | Routes::getRoutes(DocumentationPage::class)->each(function (Route $route): void { |
23 | if ($this->canAddRoute($route)) { |
24 | $this->items->put($route->getRouteKey(), NavItem::fromRoute($route)); |
25 | } |
26 | }); |
27 | |
28 | // If there are no pages other than the index page, we add it to the sidebar so that it's not empty |
29 | if ($this->items->count() === 0 && DocumentationPage::home() !== null) { |
30 | $this->items->push(NavItem::fromRoute(DocumentationPage::home(), group: 'other')); |
31 | } |
32 | } |
33 | |
34 | public function hasGroups(): bool |
35 | { |
36 | return (count($this->getGroups()) >= 1) && ($this->getGroups() !== ['other']); |
37 | } |
38 | |
39 | /** @return array<string> */ |
40 | public function getGroups(): array |
41 | { |
42 | return $this->items->map(function (NavItem $item): string { |
43 | return $item->getGroup(); |
44 | })->unique()->toArray(); |
45 | } |
46 | |
47 | /** @return Collection<\Hyde\Framework\Features\Navigation\NavItem> */ |
48 | public function getItemsInGroup(?string $group): Collection |
49 | { |
50 | return $this->items->filter(function (NavItem $item) use ($group): bool { |
51 | return ($item->getGroup() === $group) || ($item->getGroup() === Str::slug($group)); |
52 | })->sortBy('navigation.priority')->values(); |
53 | } |
54 | |
55 | public function isGroupActive(string $group): bool |
56 | { |
57 | return Str::slug(Render::getPage()->navigationMenuGroup()) === $group |
58 | || $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 | } |