|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Features\Navigation; |
|
6
|
|
|
|
|
7
|
|
|
use function collect; |
|
8
|
|
|
use function config; |
|
9
|
|
|
use Hyde\Foundation\Facades\Router; |
|
10
|
|
|
use Hyde\Support\Models\Route; |
|
11
|
|
|
use Illuminate\Support\Collection; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @see \Hyde\Framework\Testing\Feature\NavigationMenuTest |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class BaseNavigationMenu |
|
17
|
|
|
{ |
|
18
|
|
|
public Collection $items; |
|
19
|
|
|
|
|
20
|
|
|
final 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
|
|
|
Router::each(function (Route $route): void { |
|
|
|
|
|
|
34
|
|
|
$this->items->put($route->getRouteKey(), NavItem::fromRoute($route)); |
|
|
|
|
|
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
collect(config('hyde.navigation.custom', []))->each(function (NavItem $item): void { |
|
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): bool { |
|
64
|
|
|
return $this->shouldItemBeHidden($item); |
|
65
|
|
|
})->values(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function filterDuplicateItems(): Collection |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->items->unique(function (NavItem $item): string { |
|
71
|
|
|
return $item->label; |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected static function shouldItemBeHidden(NavItem $item): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return $item->hidden; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|