ldc-lionor/app/Models/Product.php
2023-08-28 08:46:47 +00:00

174 lines
4.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\CookingModeEnum;
use App\Enums\NutriscoreEnum;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasFlashStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Product extends Model
{
use HasFactory;
use HasFlashStatus;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'slug',
'seo_title',
'seo_description',
'picture',
'picture_alt',
'picture_title',
'sticker',
'sticker_alt',
'sticker_title',
'description',
'preparation',
'ingredients',
'info_cooking',
'info_consumption',
'cooking_mode',
'nutriscore',
'nutritional_values',
'status',
'range_id',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
/** @var array<ingredient> */
'nutritional_values' => 'array',
'status' => 'bool',
'cooking_mode' => CookingModeEnum::class,
'nutriscore' => NutriscoreEnum::class
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
// * ASSERTIONS
static::assertFieldIsUnique('name', $model->name);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation ?? '');
ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients);
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
});
static::created(function (self $model) {
static::flashStatus($model);
});
static::updating(function (self $model) {
// * ASSERTIONS
static::assertFieldIsUnique('name', $model->name, $model->id);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation ?? '');
ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients);
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'sticker');
static::flashStatus($model, true);
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'sticker');
});
}
// * Data structure
/**
* Get url input Template
*
* @return array
*/
public static function ingredient(): array
{
return [
/**
* @var string Eg: Valeur énergétique.
*/
'label' => '',
/**
* @var string Eg: 1076 kJ, 259 kcal.
*/
'value' => ''
];
}
// * METHODS
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $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
/**
* BelongsToMany certifications relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function certifications()
{
return $this->belongsToMany(Certification::class, 'product_certification');
}
/**
* BelongsTo certifications relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function range()
{
return $this->belongsTo(Range::class);
}
/**
* BelongsToMany recipes relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function recipes()
{
return $this->belongsToMany(Recipe::class);
}
}