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