20250711 1.1.1 Beta 3
This commit is contained in:
139
tests/data/MenuModel.php
Normal file
139
tests/data/MenuModel.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user