ldc-doux/app/Models/Picto.php
2026-10-27 09:12:51 +01:00

80 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Page;
use App\Lib\Utils;
use App\Traits\CastsEnumsHasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
class Picto extends Model
{
use HasFactory;
use CastsEnumsHasTranslations;
/**
* Map attribute names to enum classes.
*
* @var array
*/
public $enumCasts = [
'page' => Page::class
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
#[\Override]
protected $fillable = [
'picture',
'alt_text_picture',
'text',
];
/**
* The attributes that are translatable.
*
* @var array
*/
public $translatable = [
'picture',
'alt_text_picture',
'text',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function boot()
{
static::creating(function (Picto $picto): void {
if (is_a($picto->picture, "SplFileInfo")) {
$picto->picture = Utils::storeImage($picto->picture, $picto->getTable());
}
});
static::updating(function (Picto $picto): void {
if (is_a($picto->picture, "SplFileInfo")) {
$picto->picture = Utils::storeImage($picto->picture, $picto->getTable());
}
});
static::deleted(function (Picto $picto): void {
if ($picto->picture) {
$urlPicture = public_path() . $picto->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
parent::boot();
}
}