ProductSpeciesEnum::class, 'nutriscore' => NutriscoreEnum::class ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'slider_pictures' => 'array', 'packaging' => 'array', 'caliber' => 'array', 'nutritionnal_values' => 'array', 'is_frenchpoultry' => 'bool', 'is_vip' => 'bool', 'is_egalim' => 'bool', 'status' => 'bool' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { // * EVENT CLOSURES. static::creating(function (self $model) { self::manageFields($model); $model->order = \intval(self::query()->max('order')) + 1; }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'sticker_sm'); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'sticker_sm'); }); } // * METHODS /** * Undocumented function * * @param Model $model * @return void */ private static function manageFields(Model $model) { // * ASSERTIONS static::assertLabelIsUnique($model->label, $model->id); ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients); ToolboxHelper::assertNoInternalBoUrls('advantage', $model->advantage); ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation); foreach ($model->packaging as $pack) { ToolboxHelper::assertNoInternalBoUrls('packaging', $pack['value']); } foreach ($model->caliber as $calib) { ToolboxHelper::assertNoInternalBoUrls('caliber', $calib['value']); } foreach ($model->nutritionnal_values as $nutritionnal_value) { if ($model->nutritionnal_value) { ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $nutritionnal_value['value']); } } // * TRANSFORMATIONS $model->slug = Str::slug($model->label); $model->sticker_sm = FileStorageHelper::storeFile($model, $model->sticker_sm, true); $model->ingredients = clean($model->ingredients); $model->advantage = clean($model->advantage); $model->preparation = clean($model->preparation); $model->slider_pictures = clean($model->slider_pictures); $model->packaging = clean($model->packaging); $model->caliber = clean($model->caliber); $model->nutritionnal_values = clean($model->nutritionnal_values); } /** * Asserts using validation that the label is unique * * @param mixed $label * @param integer|null $id * @return void */ private static function assertLabelIsUnique($label, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id); } /** * Get associated tips * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function tipsQuery(): BelongsToMany { return $this->advices()->where('type', AdviceTypeEnum::tips()); } /** * Get associated recipes * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function recipesQuery(): BelongsToMany { return $this->advices()->where('type', AdviceTypeEnum::tips()); } /** * Get associated tips * * @return \Illuminate\Database\Eloquent\Collection<\App\Models\Advice> */ public function tips(): Collection { return $this->tipsQuery()->get(); } /** * Get associated recipes * * @return \Illuminate\Database\Eloquent\Collection<\App\Models\Advice> */ public function recipes(): Collection { return $this->recipesQuery()->get(); } // * RELATIONSHIPS /** * HasOne Subrange relation * * @return \Illuminate\Database\Eloquent\Relations\belongsTo */ public function subrange() { return $this->belongsTo(Subrange::class); } /** * BelongsToMany advices relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function advices() { return $this->belongsToMany(Advice::class, 'advice_product'); } /** * BelongsToMany advice relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function news() { return $this->belongsToMany(News::class); } }