|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Features\Navigation; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Hyde\Pages\DocumentationPage; |
|
9
|
|
|
|
|
10
|
|
|
use function min; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Abstraction for a grouped navigation menu item, like a dropdown or a sidebar group. |
|
14
|
|
|
*/ |
|
15
|
|
|
class NavigationGroup extends NavigationMenu |
|
16
|
|
|
{ |
|
17
|
|
|
protected string $label; |
|
18
|
|
|
protected int $priority; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(string $label, array $items = [], int $priority = NavigationMenu::LAST) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct($items); |
|
23
|
|
|
|
|
24
|
|
|
$this->label = $label; |
|
25
|
|
|
$this->priority = $priority; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function create(string $label, array $items = [], int $priority = NavigationMenu::LAST): static |
|
29
|
|
|
{ |
|
30
|
|
|
return new static($label, $items, $priority); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getLabel(): string |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->label; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getGroupKey(): string |
|
39
|
|
|
{ |
|
40
|
|
|
return Str::slug($this->label); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getPriority(): int |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->containsOnlyDocumentationPages()) { |
|
46
|
|
|
// For sidebar groups, we use the priority of the lowest priority child, unless the dropdown instance itself has a lower priority. |
|
47
|
|
|
return (int) min($this->priority, $this->getItems()->min(fn (NavigationItem $item): int => $item->getPriority())); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->priority; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function containsOnlyDocumentationPages(): bool |
|
54
|
|
|
{ |
|
55
|
|
|
return count($this->getItems()) && $this->getItems()->every(function (NavigationItem $item): bool { |
|
56
|
|
|
return $item->getPage() instanceof DocumentationPage; |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @experimental This method is subject to change before its release. */ |
|
61
|
|
|
public static function normalizeGroupKey(string $group): string |
|
62
|
|
|
{ |
|
63
|
|
|
return Str::slug($group); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|