124 lines
4.5 KiB
PHP
124 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\NutritionalValuesCast;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Domain\Models\ValueObjects\Products\BreedingMethod;
|
|
use Domain\Models\ValueObjects\Products\Business;
|
|
use Domain\Models\ValueObjects\Products\ProductFormat;
|
|
use Domain\Models\ValueObjects\Products\ProductRange;
|
|
use Domain\Models\ValueObjects\Products\Technology;
|
|
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
|
|
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 $category Domain\Models\ValueObjects\Products\ProductRange
|
|
* @property Technology $technology Domain\Models\ValueObjects\Products\Technology
|
|
* @property bool $is_egalim
|
|
* @property \Illuminate\Support\Collection<int, BreedingMethod> $breeding_methods Array of Domain\Models\ValueObjects\Products\BreedingMethod
|
|
* @property \Illuminate\Support\Collection<int, Business> $businesses Array of Domain\Models\ValueObjects\Products\Business
|
|
* @property \Illuminate\Support\Collection<int, ProductFormat> $formats Array of Domain\Models\ValueObjects\Products\ProductFormat
|
|
* @property string $details_and_advantages
|
|
* @property string $handling_advices
|
|
* @property string|null $ingredients
|
|
* @property string $conservation_before_opening
|
|
* @property string|null $conservation_after_opening
|
|
* @property \Domain\Models\ValueObjects\Products\NutritionalValues|null $nutritional_values Domain\Models\ValueObjects\NutritionalValues
|
|
* @property string|null $pdf
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Recipe> $recipes
|
|
*
|
|
* @method static \Database\Factories\ProductFactory factory($count = null, $state = [])
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class Product extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\ProductFactory> */
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'name',
|
|
'category',
|
|
'technology',
|
|
'is_egalim',
|
|
'breeding_methods',
|
|
'businesses',
|
|
'formats',
|
|
'details_and_advantages',
|
|
'handling_advices',
|
|
'ingredients',
|
|
'conservation_before_opening',
|
|
'conservation_after_opening',
|
|
'nutritional_values',
|
|
'pdf',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_egalim' => 'boolean',
|
|
'category' => ProductRange::class,
|
|
'technology' => Technology::class,
|
|
'breeding_methods' => AsEnumCollection::class . ':' . BreedingMethod::class,
|
|
'businesses' => AsEnumCollection::class . ':' . Business::class,
|
|
'formats' => AsEnumCollection::class . ':' . ProductFormat::class,
|
|
'nutritional_values' => NutritionalValuesCast::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');
|
|
FileStorageHelper::removeFile($model, 'pdf');
|
|
});
|
|
}
|
|
|
|
/** @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\App\Models\Recipe,$this> */
|
|
public function recipes(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Recipe::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);
|
|
if ($model->pdf) {
|
|
$model->pdf = FileStorageHelper::storeFile($model, $model->pdf, 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);
|
|
}
|
|
}
|