Skip to content

Commit

Permalink
Return attributes in toArray
Browse files Browse the repository at this point in the history
  • Loading branch information
nedwors committed Jan 2, 2025
1 parent 1710446 commit 5234789
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to `navigator` will be documented in this file

## Unreleased

## 0.8.0 2025-01-02
### Added
- Added `attributes` in `toArray`

## 0.7.0 2024-03-18
### Added
- Added support for Laravel 11, PHP 8.2, 8.3
Expand Down
12 changes: 6 additions & 6 deletions src/Facades/Nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
use Nedwors\Navigator\Item;

/**
* @method static Item item(string $name) Create a new menu item
* @method static self define(Closure $items, ?string $menu = null) Define a menu of items
* @method static Collection<Item> items(?string $menu = null) Retrieve items in the given menu
* @method static string toJson(?string $menu = null, mixed $options = 0) Retrieve items in the given menu as json, using Laravel Collection's toJson method
* @method static self filter(Closure $filter, ?string $menu = null) Define how the items should be filtered upon retrieval
* @method static self activeWhen(Closure $activeCheck, ?string $menu = null) Define what qualifies an item as active
* @method static Item item(string $name) Create a new menu item
* @method static self define(Closure $items, ?string $menu = null) Define a menu of items
* @method static Collection<Item> items(?string $menu = null) Retrieve items in the given menu
* @method static string toJson(?string $menu = null, mixed $options = 0) Retrieve items in the given menu as json, using Laravel Collection's toJson method
* @method static self filter(Closure $filter, ?string $menu = null) Define how the items should be filtered upon retrieval
* @method static self activeWhen(Closure $activeCheck, ?string $menu = null) Define what qualifies an item as active
*/
class Nav extends Facade
{
Expand Down
24 changes: 15 additions & 9 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
namespace Nedwors\Navigator;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Fluent;

/**
* @property string $name The display name for the item
* @property string $url The full url for the item
* @property ?string $heroicon The heroicon name for the item
* @property ?string $icon The icon name/path for the item
* @property-read bool $active Determine if the current item is active
* @property-read bool $available Determine if the current item passes its conditions for display
* @property-read Collection<self> $subItems Retrieve the item's sub menu items
* @property string $name The display name for the item
* @property string $url The full url for the item
* @property ?string $heroicon The heroicon name for the item
* @property ?string $icon The icon name/path for the item
* @property-read bool $active Determine if the current item is active
* @property-read bool $available Determine if the current item passes its conditions for display
* @property-read Collection<self> $subItems Retrieve the item's sub menu items
* @property-read bool $hasActiveDescendants Determine if any of the item's descendants are active
*/
class Item extends Fluent
Expand Down Expand Up @@ -109,7 +110,7 @@ public function jsonSerialize(): array
/** @return array<string, mixed> */
public function toArray(): array
{
return [
$coreAttributes = [
'name' => $this->name,
'url' => $this->url,
'icon' => $this->icon,
Expand All @@ -118,9 +119,14 @@ public function toArray(): array
'active' => $this->active,
'hasActiveDescendants' => $this->hasActiveDescendants,
];

$additionalAttributes = [
'attributes' => Arr::except($this->attributes, array_keys($coreAttributes)),
];

return array_merge($coreAttributes, $additionalAttributes);
}

/** @param mixed $name */
public function __get($name): mixed
{
return match ($name) {
Expand Down
7 changes: 5 additions & 2 deletions tests/Unit/Item/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
$item = (new Item())
->called('Dashboard')
->for('dashboard')
->heroicon('o-cog');
->heroicon('o-cog')
->foo('bar');

expect($item->toArray())->toEqual([
'name' => 'Dashboard',
Expand All @@ -29,7 +30,8 @@
'heroicon' => 'o-cog',
'subItems' => collect(),
'active' => false,
'hasActiveDescendants' => false
'hasActiveDescendants' => false,
'attributes' => ['foo' => 'bar'],
]);
});

Expand All @@ -47,6 +49,7 @@
'subItems' => collect(),
'active' => false,
'hasActiveDescendants' => false,
'attributes' => [],
]));
});

Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Nav/ToJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
'subItems' => [],
'active' => false,
'hasActiveDescendants' => false,
'attributes' => []
]
],
'active' => false,
'hasActiveDescendants' => false,
],
'active' => false,
'hasActiveDescendants' => false,
'attributes' => []
]
];

Expand Down

0 comments on commit 5234789

Please sign in to comment.