-
-
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.
Merge pull request #1630 from hydephp/restructure-navigation-item-oop…
…-design [2.x] Extract trait for shared code with navigation menu class
- Loading branch information
Showing
3 changed files
with
59 additions
and
63 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/framework/src/Framework/Features/Navigation/HasNavigationItems.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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Navigation; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* Contains shared logic for classes that have navigation items. | ||
* | ||
* @see \Hyde\Framework\Features\Navigation\NavigationMenu | ||
* @see \Hyde\Framework\Features\Navigation\NavigationGroup | ||
*/ | ||
trait HasNavigationItems | ||
{ | ||
/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> */ | ||
protected Collection $items; | ||
|
||
/** | ||
* 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\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> | ||
*/ | ||
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 (NavigationItem|NavigationGroup $item) => $item->getPriority())->values(); | ||
} | ||
|
||
/** | ||
* Add one or more navigation items to the navigation menu. | ||
* | ||
* @param \Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup|array<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> $items | ||
*/ | ||
public function add(NavigationItem|NavigationGroup|array $items): static | ||
{ | ||
foreach (Arr::wrap($items) as $item) { | ||
$this->addItem($item); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function addItem(NavigationItem|NavigationGroup $item): void | ||
{ | ||
$this->items->push($item); | ||
} | ||
} |
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