1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Features\Navigation; |
6
|
|
|
|
7
|
|
|
use Hyde\Hyde; |
8
|
|
|
use Hyde\Pages\Concerns\HydePage; |
9
|
|
|
use Hyde\Support\Models\Route; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use Stringable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstraction for a navigation menu item. Used by the NavigationMenu and DocumentationSidebar classes. |
15
|
|
|
* |
16
|
|
|
* You have a few options to construct a navigation menu item: |
17
|
|
|
* 1. You can supply a Route directly and explicit properties to the constructor |
18
|
|
|
* 2. You can use NavItem::fromRoute() to use data from the route |
19
|
|
|
* 3. You can use NavItem::toLink() for an external or un-routed link |
20
|
|
|
*/ |
21
|
|
|
class NavItem implements Stringable |
22
|
|
|
{ |
23
|
|
|
public Route $route; |
24
|
|
|
public string $href; |
25
|
|
|
|
26
|
|
|
public string $label; |
27
|
|
|
public int $priority; |
28
|
|
|
public bool $hidden; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new navigation menu item. |
32
|
|
|
* |
33
|
|
|
* @param \Hyde\Support\Models\Route|null $route |
34
|
|
|
* @param string $label |
35
|
|
|
* @param int $priority |
36
|
|
|
* @param bool $hidden |
37
|
|
|
*/ |
38
|
|
|
public function __construct(?Route $route, string $label, int $priority = 500, bool $hidden = false) |
39
|
|
|
{ |
40
|
|
|
if ($route !== null) { |
41
|
|
|
$this->route = $route; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->label = $label; |
45
|
|
|
$this->priority = $priority; |
46
|
|
|
$this->hidden = $hidden; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new navigation menu item from a route. |
51
|
|
|
*/ |
52
|
|
|
public static function fromRoute(Route $route): static |
53
|
|
|
{ |
54
|
|
|
return new self( |
55
|
|
|
$route, |
56
|
|
|
$route->getPage()->navigationMenuLabel(), |
57
|
|
|
$route->getPage()->navigationMenuPriority(), |
58
|
|
|
! $route->getPage()->showInNavigation() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create a new navigation menu item leading to an external URI. |
64
|
|
|
*/ |
65
|
|
|
public static function toLink(string $href, string $label, int $priority = 500): static |
66
|
|
|
{ |
67
|
|
|
return (new self(null, $label, $priority, false))->setDestination($href); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a new navigation menu item leading to a Route model. |
72
|
|
|
*/ |
73
|
|
|
public static function toRoute(Route $route, string $label, int $priority = 500): static |
74
|
|
|
{ |
75
|
|
|
return new self($route, $label, $priority, false); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Resolve a link to the navigation item. |
80
|
|
|
*/ |
81
|
|
|
public function resolveLink(): string |
82
|
|
|
{ |
83
|
|
|
return $this->href ?? $this->route->getLink(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Resolve a link to the navigation item. |
88
|
|
|
*/ |
89
|
|
|
public function __toString(): string |
90
|
|
|
{ |
91
|
|
|
return $this->resolveLink(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if the NavItem instance is the current page. |
96
|
|
|
*/ |
97
|
|
|
public function isCurrent(?HydePage $current = null): bool |
98
|
|
|
{ |
99
|
|
|
if ($current === null) { |
100
|
|
|
$current = Hyde::currentRoute()->getPage(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (! isset($this->route)) { |
104
|
|
|
return ($current->getRoute()->getRouteKey() === $this->href) |
105
|
|
|
|| ($current->getRoute()->getRouteKey().'.html' === $this->href); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $current->getRoute()->getRouteKey() === $this->route->getRouteKey(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function setDestination(string $href): static |
112
|
|
|
{ |
113
|
|
|
$this->href = $href; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setPriority(int $priority): static |
119
|
|
|
{ |
120
|
|
|
$this->priority = $priority; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getGroup(): ?string |
126
|
|
|
{ |
127
|
|
|
return $this->normalizeGroupKey($this->route->getPage()->data('navigation.group')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getRoute(): ?Route |
131
|
|
|
{ |
132
|
|
|
return $this->route ?? null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function normalizeGroupKey(?string $group): ?string |
136
|
|
|
{ |
137
|
|
|
return empty($group) ? null : Str::slug($group); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|