-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90a96e3
commit 9ccc1a2
Showing
3 changed files
with
64 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/framework/src/Framework/Features/Navigation/NavigationMenu.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Navigation; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
|
||
/** | ||
* Represents a site navigation menu, and contains all of its navigation items. | ||
* | ||
* The automatic navigation menus are stored within the service container and can be resolved by their identifiers. | ||
* | ||
* @example `$menu = app('navigation')->getMenu('main');` for the main navigation menu. | ||
* @example `$menu = app('navigation')->getMenu('sidebar');` for the documentation sidebar. | ||
*/ | ||
abstract class NavigationMenu | ||
{ | ||
/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavItem> */ | ||
protected Collection $items; | ||
|
||
/** | ||
* Create a new navigation menu instance. | ||
*/ | ||
public function __construct(Arrayable|array $items = []) | ||
{ | ||
$this->items = new Collection(); | ||
|
||
foreach ($items as $item) { | ||
// Instead of adding the items directly, we iterate through them and | ||
// add them through the helper. This ensures that all the items are | ||
// of the correct type, bringing type safety to the menu's items. | ||
|
||
$this->add($item); | ||
} | ||
} | ||
|
||
/** | ||
* Get the navigation items in the menu. | ||
* | ||
* Items are automatically sorted by their priority, falling back to the order they were added. | ||
* | ||
* @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavItem> | ||
*/ | ||
public function getItems(): Collection | ||
{ | ||
// The reason we sort them here is that navigation items can be added from different sources, | ||
// so any sorting we do in generator actions will only be partial. This way, we can ensure | ||
// that the items are always freshly sorted by their priorities when they are retrieved. | ||
|
||
return $this->items->sortBy(fn (NavItem $item) => $item->getPriority())->values(); | ||
} | ||
|
||
/** | ||
* Add a navigation item to the navigation menu. | ||
*/ | ||
public function add(NavItem $item): void | ||
{ | ||
$this->items->push($item); | ||
} | ||
} |