Files
michaelschiemer/src/Application/Admin/ValueObjects/NavigationItem.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

73 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Application\Admin\ValueObjects;
use App\Framework\Icon\Icon;
final readonly class NavigationItem
{
public function __construct(
public string $name,
public string $url,
public Icon|string|null $icon = null,
public bool $isActive = false
) {
}
/**
* 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
{
return new self(
name: $data['name'] ?? '',
url: $data['url'] ?? '',
icon: $data['icon'] ?? null, // Can be string or Icon
isActive: $data['is_active'] ?? false
);
}
public function toArray(): array
{
$icon = $this->getIcon();
return [
'name' => $this->name,
'url' => $this->url,
'icon' => $this->icon instanceof Icon ? $this->icon->toString() : $this->icon,
'icon_html' => $icon?->toHtml('admin-nav__icon') ?? '',
'is_active' => $this->isActive,
];
}
public function withActiveState(bool $isActive): self
{
return new self(
name: $this->name,
url: $this->url,
icon: $this->icon,
isActive: $isActive
);
}
public function isActive(string $currentPath): bool
{
return $this->url === $currentPath || str_starts_with($currentPath, $this->url);
}
}