2025-07-11 19:40:30 +03:00

139 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace goodboyalex\php_components_pack\tests\data;
use Exception;
use goodboyalex\php_components_pack\classes\ClassMapper;
use goodboyalex\php_components_pack\extensions\GUIDExtension;
use goodboyalex\php_components_pack\interfaces\IDuplicated;
use goodboyalex\php_components_pack\interfaces\IStoredAtSQL;
/**
* Модель меню.
*
* @author Александр Бабаев
* @package freecms
* @version 0.1
* @since 0.1
*/
final class MenuModel implements IDuplicated, IStoredAtSQL
{
/**
* @var string $Id Идентификатор меню.
*/
public string $Id {
get {
return $this->Id ?? GUIDExtension::GUIDEmpty;
}
set {
// Проверка идентификатора
if (!GUIDExtension::Validate($value))
// - исключение
throw new Exception("Неверный идентификатор (GUID)");
// Установка идентификатора
$this->Id = $value;
}
}
/**
* @var string $Name Имя меню.
*/
public string $Name = "";
/**
* @var string $Description Описание меню.
*/
public string $Description = "";
/**
* @var MenuItems Элементы меню.
*/
public MenuItems $Items;
/**
* @var bool $AllowSubMenu Допустимо ли вложенное меню.
*/
public bool $AllowSubMenu = true;
/**
* @var MenuCssClassModel $Css Модель стилей оформления меню.
*/
public MenuCssClassModel $Css;
/**
* Конструктор.
*/
public function __construct ()
{
$this->Items = new MenuItems([]);
$this->Css = new MenuCssClassModel();
}
/**
* @inheritdoc
*/
public function Duplicate (): MenuModel
{
// Создаю новый класс
$model = new MenuModel();
// Копирую свойства класса
ClassMapper::MapClass($this, $model);
// Возвращаю класс
return $model;
}
/**
* @inheritDoc
*/
public function ToSQL (bool $withId = true): array
{
// Создаем результат
$result = [];
// Добавляем значения
$result['Name'] = $this->Name;
$result['Description'] = $this->Description;
$result['Items'] = $this->Items->Serialize();
$result['AllowSubMenu'] = $this->AllowSubMenu ? 1 : 0;
$result['Css'] = serialize($this->Css);
// Если требуется идентификатор
if ($withId)
// - то добавляем его
$result['Id'] = $this->Id;
// Возвращаем результат
return $result;
}
/**
* @inheritDoc
*/
public function FromSQL (array $sqlData): MenuModel
{
// Создаем результат
$result = new MenuModel();
// Устанавливаем значения
if (isset($sqlData['Id']))
$result->Id = $sqlData['Id'];
if (isset($sqlData['Name']))
$result->Name = $sqlData['Name'];
if (isset($sqlData['Description']))
$result->Description = $sqlData['Description'];
$this->Items = new MenuItems([]);
if (isset($sqlData['Items']))
$result->Items->UnSerialize($sqlData['Items']);
if (isset($sqlData['AllowSubMenu']))
$result->AllowSubMenu = $sqlData['AllowSubMenu'] == 1;
$this->Css = new MenuCssClassModel();
if (isset($sqlData['Css']))
$result->Css = unserialize($sqlData['Css']);
// Возвращаем массив
return $result;
}
}