'bool', 'button_targetblank' => 'bool' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { if (count(self::get()) >= 4) { self::assertNumberOfSlides(); } 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, 'picture'); FileStorageHelper::removeOldFile($model, 'picture_thumbnail'); }); static::deleting(function () { if (1 >= count(self::get())) { self::assertNumberOfSlides(); } }); 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::assertTitleIsUnique($model->title, $model->id); ToolboxHelper::assertNoInternalBoUrls($model->content, 'content'); $model->content = clean($model->content); if ($model->button_url) { ToolboxHelper::assertNoInternalBoUrls($model->button_url, 'button_url'); $model->button_url = clean($model->button_url); } $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); $model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true); } /** * Asserts the number of slides in the slider. * * @return void * @throws \Illuminate\Validation\ValidationException If there is less 1 or more than 4 slides. */ private static function assertNumberOfSlides() { throw ValidationException::withMessages([ 'Opération impossible, minimum un et maximum quatre slides doivent être présentes.' ]); } /** * Asserts using validation that the title is unique. * * @param mixed $title * @param integer|null $id * @return void */ private static function assertTitleIsUnique($title, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id); } }