|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Models\Navigation; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Contracts\RouteContract; |
|
6
|
|
|
use Hyde\Framework\Hyde; |
|
7
|
|
|
use Hyde\Framework\Models\Route; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @see \Hyde\Framework\Testing\Feature\NavigationMenuTest |
|
12
|
|
|
* @phpstan-consistent-constructor |
|
13
|
|
|
*/ |
|
14
|
|
|
class NavigationMenu |
|
15
|
|
|
{ |
|
16
|
|
|
public RouteContract $currentRoute; |
|
17
|
|
|
|
|
18
|
|
|
public Collection $items; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->items = new Collection(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function create(): static |
|
26
|
|
|
{ |
|
27
|
|
|
return (new static())->generate()->filter()->sort(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @return $this */ |
|
31
|
|
|
public function generate(): static |
|
32
|
|
|
{ |
|
33
|
|
|
Hyde::routes()->each(function (Route $route) { |
|
34
|
|
|
$this->items->push(NavItem::fromRoute($route)); |
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
collect(config('hyde.navigation.custom', []))->each(function (NavItem $item) { |
|
38
|
|
|
$this->items->push($item); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @return $this */ |
|
45
|
|
|
public function filter(): static |
|
46
|
|
|
{ |
|
47
|
|
|
$this->items = $this->filterHiddenItems(); |
|
48
|
|
|
$this->items = $this->filterDuplicateItems(); |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @return $this */ |
|
54
|
|
|
public function sort(): static |
|
55
|
|
|
{ |
|
56
|
|
|
$this->items = $this->items->sortBy('priority')->values(); |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function filterHiddenItems(): Collection |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->items->reject(function (NavItem $item) { |
|
64
|
|
|
return $item->hidden; |
|
65
|
|
|
})->values(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function filterDuplicateItems(): Collection |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->items->unique(function (NavItem $item) { |
|
71
|
|
|
return $item->resolveLink(); |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|