104 lines
3.4 KiB
PHP
104 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Domain\Models\ValueObjects\Recipes\Inspiration;
|
|
use Domain\Models\ValueObjects\Recipes\Season;
|
|
use Domain\Models\ValueObjects\Products\ProductRange;
|
|
use Domain\Models\ValueObjects\Recipes\ConsumptionTime;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string|null $seo_title
|
|
* @property string|null $seo_description
|
|
* @property string|null $picture
|
|
* @property string|null $picture_alt
|
|
* @property string|null $picture_title
|
|
* @property string $name
|
|
* @property string $slug
|
|
* @property ProductRange $product_range Domain\Models\ValueObjects\Products\ProductRange
|
|
* @property ConsumptionTime $consomption_time Domain\Models\ValueObjects\Recipes\ConsumptionTime
|
|
* @property Inspiration $inspiration Domain\Models\ValueObjects\Recipes\Inspiration
|
|
* @property Season $season Domain\Models\ValueObjects\Recipes\Season
|
|
* @property string|null $description
|
|
* @property string|null $details
|
|
* @property string $ingredients_list
|
|
* @property string $preparation_steps
|
|
* @property string|null $tips_and_advices
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Product> $products
|
|
* @method static \Database\Factories\RecipeFactory factory($count = null, $state = [])
|
|
* @mixin \Eloquent
|
|
*/
|
|
class Recipe extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\RecipeFactory> */
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'name',
|
|
'product_range',
|
|
'consomption_time',
|
|
'inspiration',
|
|
'season',
|
|
'description',
|
|
'details',
|
|
'ingredients_list',
|
|
'preparation_steps',
|
|
'tips_and_advices'
|
|
];
|
|
|
|
protected $casts = [
|
|
'product_range' => ProductRange::class,
|
|
'consomption_time' => ConsumptionTime::class,
|
|
'inspiration' => Inspiration::class,
|
|
'season' => Season::class,
|
|
];
|
|
|
|
#[\Override]
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function (self $model): void {
|
|
self::manageFields($model);
|
|
});
|
|
|
|
static::deleting(function (self $model): void {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
});
|
|
}
|
|
|
|
/** @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\App\Models\Product,$this> */
|
|
public function products(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Product::class);
|
|
}
|
|
|
|
private static function manageFields(self $model): void
|
|
{
|
|
self::assertNameIsUnique($model->name, $model->id);
|
|
|
|
$model->slug = Str::slug($model->name);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
private static function assertNameIsUnique(?string $name, ?int $id = null): void
|
|
{
|
|
$table = new self()->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name ?? ''), $id);
|
|
}
|
|
}
|