147 lines
4 KiB
PHP
147 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
|
|
|
|
/**
|
|
* @property \App\Enums\StaticPageType $type Page type (StaticPageEnum).
|
|
* @property string $seo_title Seo title about 70 chars as recommended.
|
|
* @property string $seo_description Seo description about 160 chars as recommended.
|
|
* @property string $title Title for the page.
|
|
* @property string $slug Slug of the title for the page.
|
|
* @property array $data MINICRUD - Page content.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \Illuminate\Database\Eloquent\Relations\MorphMany $sections Get Sections of the static page.
|
|
*/
|
|
class StaticPage extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'content',
|
|
'type',
|
|
'slug',
|
|
'created_at',
|
|
'updated_at'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => StaticPageType::class,
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertTitleIsUnique($model->title);
|
|
|
|
// * CHANGES
|
|
$model->slug = Str::slug($model->title);
|
|
});
|
|
static::updating(function (self $model) {
|
|
|
|
// * ASSERTIONS
|
|
self::assertTitleIsUnique($model->title, $model->id);
|
|
|
|
// * CHANGES
|
|
$model->slug = Str::slug($model->title);
|
|
|
|
self::resetCache($model->type);
|
|
});
|
|
}
|
|
|
|
// * 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';
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique
|
|
*
|
|
* @param string|null $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an actu title already exists.
|
|
*/
|
|
private static function assertTitleIsUnique(string|null $title, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @param \App\Enums\StaticPageType $type
|
|
* @return void
|
|
*/
|
|
private static function resetCache(StaticPageType $type): void
|
|
{
|
|
Cache::forget(sprintf('%sStaticPage', $type->name));
|
|
self::getCachedStaticPage($type);
|
|
}
|
|
|
|
/**
|
|
* Get cached static page associated to a given type.
|
|
*
|
|
* @param \App\Enums\StaticPageType $type
|
|
* @return \App\Models\StaticPage
|
|
*/
|
|
public static function getCachedStaticPage(StaticPageType $type): \App\Models\StaticPage
|
|
{
|
|
return Cache::rememberForever(
|
|
sprintf('%sStaticPage', $type->name),
|
|
fn() => self::query()->where('type', $type)->first()
|
|
);
|
|
}
|
|
}
|