Skip to content

Commit 544769a

Browse files
authored
Merge pull request #1645 from hydephp/restructure-navigation-item-oop-design
[2.x] Merge trait back into menu class and make navigation groups extend the menu class
2 parents f80795f + cc803ba commit 544769a

File tree

3 files changed

+42
-64
lines changed

3 files changed

+42
-64
lines changed

packages/framework/src/Framework/Features/Navigation/HasNavigationItems.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/framework/src/Framework/Features/Navigation/NavigationGroup.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Hyde\Framework\Features\Navigation;
66

77
use Illuminate\Support\Str;
8-
use Illuminate\Support\Collection;
98
use Hyde\Pages\DocumentationPage;
109

1110
use function min;
@@ -14,20 +13,17 @@
1413
/**
1514
* Abstraction for a grouped navigation menu item, like a dropdown or a sidebar group.
1615
*/
17-
class NavigationGroup
16+
class NavigationGroup extends NavigationMenu
1817
{
19-
use HasNavigationItems;
20-
2118
protected string $label;
2219
protected int $priority;
2320

2421
public function __construct(string $label, array $items = [], int $priority = NavigationMenu::LAST)
2522
{
26-
$this->items = new Collection();
23+
parent::__construct($items);
24+
2725
$this->label = $label;
2826
$this->priority = $priority;
29-
30-
$this->add($items);
3127
}
3228

3329
public static function create(string $label, array $items = [], int $priority = NavigationMenu::LAST): static

packages/framework/src/Framework/Features/Navigation/NavigationMenu.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7+
use Illuminate\Support\Arr;
78
use Illuminate\Support\Collection;
89
use Illuminate\Contracts\Support\Arrayable;
910

@@ -19,15 +20,51 @@
1920
*/
2021
class NavigationMenu
2122
{
22-
use HasNavigationItems;
23-
2423
public const DEFAULT = 500;
2524
public const LAST = 999;
2625

26+
/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> */
27+
protected Collection $items;
28+
2729
public function __construct(Arrayable|array $items = [])
2830
{
2931
$this->items = new Collection();
3032

3133
$this->add(evaluate_arrayable($items));
3234
}
35+
36+
/**
37+
* Get the navigation items in the menu.
38+
*
39+
* Items are automatically sorted by their priority, falling back to the order they were added.
40+
*
41+
* @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup>
42+
*/
43+
public function getItems(): Collection
44+
{
45+
// The reason we sort them here is that navigation items can be added from different sources,
46+
// so any sorting we do in generator actions will only be partial. This way, we can ensure
47+
// that the items are always freshly sorted by their priorities when they are retrieved.
48+
49+
return $this->items->sortBy(fn (NavigationItem|NavigationGroup $item) => $item->getPriority())->values();
50+
}
51+
52+
/**
53+
* Add one or more navigation items to the navigation menu.
54+
*
55+
* @param \Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup|array<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> $items
56+
*/
57+
public function add(NavigationItem|NavigationGroup|array $items): static
58+
{
59+
foreach (Arr::wrap($items) as $item) {
60+
$this->addItem($item);
61+
}
62+
63+
return $this;
64+
}
65+
66+
protected function addItem(NavigationItem|NavigationGroup $item): void
67+
{
68+
$this->items->push($item);
69+
}
3370
}

0 commit comments

Comments
 (0)