Files
michaelschiemer/src/Application/Admin/ValueObjects/NavigationSection.php
Michael Schiemer c93d3f07a2
All checks were successful
Test Runner / test-php (push) Successful in 31s
Deploy Application / deploy (push) Successful in 1m42s
Test Runner / test-basic (push) Successful in 7s
fix(Console): add void as valid return type for command methods
The MethodSignatureAnalyzer was rejecting command methods with void return
type, causing the schedule:run command to fail validation.
2025-11-26 06:16:09 +01:00

97 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Application\Admin\ValueObjects;
use App\Framework\Icon\Icon;
final readonly class NavigationSection
{
/** @param array<NavigationItem> $items */
public function __construct(
public string $name,
public array $items,
public Icon|string|null $icon = null
) {
}
/**
* Get icon as Icon object
*/
public function getIcon(): ?Icon
{
if ($this->icon === null) {
return null;
}
if ($this->icon instanceof Icon) {
return $this->icon;
}
return Icon::fromString($this->icon);
}
public static function fromArray(array $data): self
{
$items = [];
$itemsData = $data['items'] ?? [];
foreach ($itemsData as $key => $itemData) {
// Handle legacy format: ['Item Name' => '/url']
if (is_string($itemData)) {
$items[] = NavigationItem::fromArray([
'name' => is_string($key) ? $key : 'Item',
'url' => $itemData,
]);
} elseif (is_array($itemData)) {
// Handle modern format: [['name' => 'Item Name', 'url' => '/url']]
$items[] = NavigationItem::fromArray($itemData);
}
// Skip invalid formats
}
return new self(
name: $data['section'] ?? $data['name'] ?? '',
items: $items,
icon: $data['icon'] ?? null // Can be string or Icon
);
}
public function toArray(): array
{
$icon = $this->getIcon();
return [
'section' => $this->name,
'name' => $this->name,
'items' => array_map(
fn (NavigationItem $item) => $item->toArray(),
$this->items
),
'icon' => $this->icon instanceof Icon ? $this->icon->toString() : $this->icon,
'icon_html' => $icon?->toHtml('admin-nav__section-icon') ?? '',
];
}
public function addItem(NavigationItem $item): self
{
return new self(
name: $this->name,
items: [...$this->items, $item],
icon: $this->icon
);
}
public function hasActiveItem(string $currentPath): bool
{
foreach ($this->items as $item) {
if ($item->isActive($currentPath)) {
return true;
}
}
return false;
}
}