ldc-legaulois/app/Models/GeneralSetting.php

163 lines
4.5 KiB
PHP
Raw Normal View History

2024-04-09 12:31:45 +02:00
<?php
namespace App\Models;
use App\Enums\Settings\MenuNodeType;
use App\Enums\Settings\SettingsType;
use App\Lib\Definitions\MenuNode;
use App\Lib\Link;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
2024-04-09 12:31:45 +02:00
/**
* @property-read \App\Enums\Settings\SettingsType $type
* ! Note complete this with pipes when adding another SettingsType.
* @property array<\App\Lib\Definitions\MenuNode> $value
* @package App\Models
*/
class GeneralSetting extends Model
{
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'type';
/**
* The "type" of the primary key ID.
*
* @var string
*/
protected $keyType = SettingsType::class;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var boolean
*/
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'value',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => SettingsType::class,
'value' => 'json'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function () {
throw new \ErrorException('Not allowed to create an instance of this');
});
static::updated(fn (self $model) => Cache::forget($model->getTable()));
2024-04-09 12:31:45 +02:00
static::deleting(function () {
throw new \ErrorException('Not allowed to create an instance of this');
});
}
// * ROUTABLE
/**
* Get the value of the model's route key.
*
* @return mixed
*/
public function getRouteKey()
{
/** @var \App\Enums\StaticPageType */
$enum = $this->getAttribute($this->getRouteKeyName());
return $enum->value();
}
/**
* Get the route key for the model.
*
* ! Type is the routable parameter
*
* @return string
*/
public function getRouteKeyName(): string
{
return 'type';
}
// * GETTERS
/**
* Mutator for 'value' attribute
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
public function value(): Attribute
{
return Attribute::make(
get: fn ($value): array => self::valueFromJson($this->type, $value),
);
}
// * METHODS
/**
* Get the json layout for a setting type.
*
* @param \App\Enums\Settings\SettingsType $type
*
* ! Note complete this with pipes when adding another SettingsType.
* @return array<\App\Lib\Definitions\MenuNode>
* @throws \ErrorException If $type is unhandled.
*/
public static function getJsonLayout(SettingsType $type): array
{
return match ($type) {
SettingsType::mainMenu => collect(MenuNodeType::all())
->filter(fn (MenuNodeType $menuNodeType) => $menuNodeType !== MenuNodeType::link)
->map(fn (MenuNodeType $nodeType) => MenuNode::make(
$nodeType->value(),
!empty($nodeType->routeName()) ?
// phpcs:ignore Generic.Files.LineLength.TooLong
(new Link(routeName: $nodeType->routeName()))->toArray() : (new Link(url: 'https://example.net'))->toArray()
))->all(),
// @phpstan-ignore-next-line
default => throw new \ErrorException("No instruction to seed setting type '{$type->label()}'")
};
}
/**
* Unserialize setting value
*
* @param \App\Enums\Settings\SettingsType $type
* @param string $json
* ! Note complete this with pipes when adding another SettingsType.
* @return array<\App\Lib\Definitions\MenuNode>
* @throws \ErrorException If $type is unhandled.
*/
public static function valueFromJson(SettingsType $type, string $json): array
{
return match ($type) {
SettingsType::mainMenu => collect(json_decode($json, true))
->map(fn (array $jsonLayout) => MenuNode::make(...$jsonLayout))->all(),
// @phpstan-ignore-next-line
default => throw new \ErrorException("No instruction to unserialize setting type '{$type->label()}'")
};
}
}