ldc-legaulois/app/Lib/Definitions/MenuNode.php

74 lines
2.2 KiB
PHP
Raw Permalink Normal View History

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