180 lines
6.3 KiB
PHP
180 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Domain\Models\ValueObjects\Entities\RecipeId;
|
|
use Domain\Models\ValueObjects\Products\ProductCategory;
|
|
use Domain\Models\ValueObjects\Recipes\ConsumptionTime;
|
|
use Domain\Models\ValueObjects\Recipes\Inspiration;
|
|
use Domain\Models\ValueObjects\Recipes\Season;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Scout\Searchable;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Spatie\Translatable\HasTranslations;
|
|
use Uwil\LaravelBackend\Interfaces\TranslatableModel;
|
|
use Uwil\LaravelBackend\Traits\Models\HasActivityLog;
|
|
use Uwil\LaravelBackend\Traits\Models\HasCache;
|
|
use Uwil\LaravelBackend\Traits\Models\HasModelNaming;
|
|
use Uwil\LaravelBackend\Traits\Models\HasUwilImages;
|
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong
|
|
|
|
/**
|
|
* @phpcs :disable Generic.Files.LineLength.TooLong
|
|
*
|
|
* @property string $id
|
|
* @property string $seo_title
|
|
* @property string $seo_description
|
|
* @property string $picture
|
|
* @property string $picture_alt
|
|
* @property string $picture_title
|
|
* @property string $name
|
|
* @property string $slug
|
|
* @property ProductCategory $product_category 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 $deleted_at
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Uwil\LaravelBackend\Models\ActivityLog> $activityLogs
|
|
* @property-read array $translatable_columns_from
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Product> $products
|
|
* @property-read mixed $translations
|
|
*
|
|
* @method static \Database\Factories\RecipeFactory factory($count = null, $state = [])
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Recipe onlyTrashed()
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Recipe whereJsonContainsLocale(string $column, string $locale, ?mixed $value, string $operand = '=')
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Recipe whereJsonContainsLocales(string $column, array $locales, ?mixed $value, string $operand = '=')
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Recipe whereLocale(string $column, string $locale)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Recipe whereLocales(string $column, array $locales)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Recipe withTrashed(bool $withTrashed = true)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|Recipe withoutTrashed()
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
// phpcs:enable Generic.Files.LineLength.TooLong
|
|
class Recipe extends Model implements TranslatableModel
|
|
{
|
|
use HasActivityLog;
|
|
use HasCache;
|
|
|
|
/** @use HasFactory<\Database\Factories\RecipeFactory> */
|
|
use HasFactory;
|
|
|
|
use HasModelNaming;
|
|
use HasTranslations;
|
|
use HasUuids;
|
|
use HasUwilImages;
|
|
use Searchable;
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'name',
|
|
'product_category',
|
|
'consomption_time',
|
|
'inspiration',
|
|
'season',
|
|
'description',
|
|
'details',
|
|
'ingredients_list',
|
|
'preparation_steps',
|
|
'tips_and_advices',
|
|
];
|
|
|
|
protected $casts = [
|
|
'product_category' => ProductCategory::class,
|
|
'consomption_time' => ConsumptionTime::class,
|
|
'inspiration' => Inspiration::class,
|
|
'season' => Season::class,
|
|
];
|
|
|
|
public function newUniqueId(): string
|
|
{
|
|
return Uuid::uuid7()->toString();
|
|
}
|
|
|
|
public function getId(): RecipeId
|
|
{
|
|
return new RecipeId(Uuid::fromString($this->id));
|
|
}
|
|
|
|
#[\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);
|
|
}
|
|
|
|
/**
|
|
* Relations should be share to views.
|
|
*
|
|
* @return array<int,string>
|
|
*/
|
|
public static function getPropsRelations(): array
|
|
{
|
|
return self::getCacheRelations();
|
|
}
|
|
|
|
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, string | int | null $id = null): void
|
|
{
|
|
$table = new self()->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name ?? ''), $id);
|
|
}
|
|
|
|
/**
|
|
* Get the indexable data array for the model.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toSearchableArray(): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->name,
|
|
'product_category' => $this->product_category?->value,
|
|
'consomption_time' => $this->consomption_time?->value,
|
|
'inspiration' => $this->inspiration?->value,
|
|
'season' => $this->season?->value,
|
|
];
|
|
}
|
|
}
|