*/
protected $fillable = [
'sub_label',
'title',
'text',
'text_color',
'link_url',
'link_title',
'link_target',
'link_status',
'picture',
'picture_alt',
'picture_title',
'picture_status',
'background_picture',
'background_picture_alt',
'background_picture_title',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'type' => MenuNodeType::class,
'text_color' => HtmlColor::class,
'link_target' => 'boolean',
'link_status' => 'boolean',
'picture_status' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
* @throws \ErrorException Not allowed to create an instance of this.
*/
protected static function booted(): void
{
static::creating(fn () => throw new \ErrorException('Not allowed to create an instance of this'));
static::updating(function (self $model) {
self::denyHandlingSomeTypes($model);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->background_picture = FileStorageHelper::storeFile($model, $model->background_picture, true);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'background_picture');
self::removeCached();
});
static::deleting(fn () => throw new \ErrorException('Not allowed to create an instance of this'));
}
// * METHODS
/**
* Deny Handling some of the MenuNodeData
*
* @param self $model
* @return void
* @throws \RuntimeException Database operations are not allowed for some types.
*/
private static function denyHandlingSomeTypes(self $model): void
{
if (in_array($model->type, [MenuNodeType::siteLogo, MenuNodeType::link])) {
throw new \RuntimeException('Database operations are not allowed the type ' . $model->type->label());
}
}
}