ldc-nature-and-respect/app/Models/Recipe.php
Alexis Gousseau a333993456 add seeders
2023-03-16 09:47:16 +01:00

153 lines
4 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Recipes\DifficultyEnum;
use App\Enums\Recipes\CookingEnum;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id
* @property integer $range_id
* @property string $seo_title
* @property string $seo_description
* @property string $name
* @property string $slug
* @property string $picture
* @property string $picture_thumbnail
* @property string $ingredients
* @property string $teaser
* @property string $steps
* @property boolean $difficulty
* @property boolean $preparation
* @property boolean $duration
* @property string $astuces
* @property boolean $cooking
* @property boolean $status
* @property integer $order
* @property string $created_at
* @property string $updated_at
* @property Product[] $products
* @property Range $range
*/
class Recipe extends Model
{
use HasFactory;
/**
* @var array
*/
protected $fillable = [
'range_id',
'seo_title',
'seo_description',
'name',
'slug',
'picture',
'picture_thumbnail',
'ingredients',
'teaser',
'steps',
'difficulty',
'preparation',
'duration',
'astuces',
'cooking',
'status',
'order',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'ingredients' => 'json',
'steps' => 'json',
'difficulty' => DifficultyEnum::class,
'cooking' => CookingEnum::class,
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'picture_thumbnail');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'picture_thumbnail');
});
}
// * METHODS
/**
* Undocumented function
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
self::assertFieldIsUnique('name', $model->name);
self::assertFieldIsUnique('slug', $model->slug);
ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients);
ToolboxHelper::assertNoInternalBoUrls('steps', $model->steps);
$model->ingredients = clean($model->ingredients);
$model->steps = clean($model->steps);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
}
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param mixed $valueField
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
return $this->belongsToMany('App\Models\Product', 'recipes_products');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function range()
{
return $this->belongsTo('App\Models\Range');
}
}