Skip to content

Commit

Permalink
Merge pull request #1630 from hydephp/restructure-navigation-item-oop…
Browse files Browse the repository at this point in the history
…-design

[2.x] Extract trait for shared code with navigation menu class
  • Loading branch information
caendesilva authored Mar 28, 2024
2 parents 62d6930 + bccde3d commit f80795f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 63 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Hyde\Framework\Features\Navigation;

use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Hyde\Pages\DocumentationPage;

Expand All @@ -17,8 +16,8 @@
*/
class NavigationGroup
{
/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem> */
protected Collection $items;
use HasNavigationItems;

protected string $label;
protected int $priority;

Expand Down Expand Up @@ -56,27 +55,6 @@ public function getPriority(): int
return $this->priority;
}

/** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem> */
public function getItems(): Collection
{
return $this->items->sortBy(fn (NavigationItem|NavigationGroup $item) => $item->getPriority())->values();
}

/** @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);
}

protected function containsOnlyDocumentationPages(): bool
{
return count($this->getItems()) && collect($this->getItems())->every(function (NavigationItem $item): bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Hyde\Framework\Features\Navigation;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Arrayable;

Expand All @@ -20,51 +19,15 @@
*/
class NavigationMenu
{
use HasNavigationItems;

public const DEFAULT = 500;
public const LAST = 999;

/** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavigationItem|\Hyde\Framework\Features\Navigation\NavigationGroup> */
protected Collection $items;

public function __construct(Arrayable|array $items = [])
{
$this->items = new Collection();

$this->add(evaluate_arrayable($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);
}
}

0 comments on commit f80795f

Please sign in to comment.