ldc-legaulois/app/Lib/Definitions/MenuNode.php
2026-09-25 16:25:31 +02:00

73 lines
2.2 KiB
PHP

<?php
namespace App\Lib\Definitions;
use App\Enums\Settings\MenuNodeType;
use App\Lib\Link;
/**
* @property-read int $id The node menu id.
* @property-read \App\Enums\Settings\MenuNodeType $type The node menu type.
* @property-read string $title The node menu title.
* @property-read \App\Lib\Link $link The node menu link.
* @property-read bool $published The node menu publish status.
*/
class MenuNode extends \stdClass
{
/**
* Get property.
*
* @throws \TypeError If prop is undefined.
*/
public function __get(string $name): mixed
{
if (in_array($name, ['id', 'type', 'title', 'link', 'published'])) {
return $this->{$name};
}
throw new \TypeError("undefined property {$name}");
}
/**
* Make and instance
*
* @param int|null $id The node menu id.
* @param int $type The node menu type.
* @param bool $published The node menu published.
* @param string $title The node menu title.
* @param array $link The node menu link.
*
* @throws \RuntimeException If $type or $data is wrong.
*/
public static function make(?int $id, int $type, bool $published, string $title, array $link): self
{
self::assertTypeIsCorrect($type);
$menuNode = new self();
// @phpstan-ignore-next-line
$menuNode->id = ! is_null($id) ? $id : 1;
// @phpstan-ignore-next-line
$menuNode->type = MenuNodeType::make($type);
// @phpstan-ignore-next-line
$menuNode->published = $published;
// @phpstan-ignore-next-line
$menuNode->title = $title;
// @phpstan-ignore-next-line
$menuNode->link = new Link(...$link);
return $menuNode;
}
/**
* Assert Type is correct
*
* @throws \RuntimeException If $type is wrong.
*/
public static function assertTypeIsCorrect(int $type): void
{
try {
$type = MenuNodeType::make($type);
} catch (\BadMethodCallException $e) {
throw new \RuntimeException($e->getMessage());
}
}
}