merge([ 'type' => intval($this->route()->parameter('news_club_type')), 'content' => ($this->type === NewsClubTypeEnum::news->value()) ? $this->input('content') : null, 'multiple_answers' => $this->boolean('multiple_answers'), 'published' => $this->boolean('published'), ]); $this->mergePicture('picture'); } /** * Get the validation rules that apply to the request. */ public function rules(): array { $rules = [ 'type' => ['required', new Enum(NewsClubTypeEnum::class)], 'title' => 'required|string|between:3,255', 'picture_alt' => 'required|string|between:3,255', 'picture_title' => 'required|string|between:3,255', 'content' => ['nullable', 'string', new WysiwygLength()], 'multiple_answers' => 'required|boolean', 'published' => [ 'required', 'boolean', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, string $value, $fail) { if ( ($publishedModels = NewsClub::query() ->where('published', true) ->orderBy('publish_at', 'asc') ->get())->count() >= 3 && ! $publishedModels->contains(request()->route('news_club')) && $this->published ) { $publishedModels->first()->update(['published' => false]); // phpcs:ignore Generic.Files.LineLength.TooLong Session::flash('warning', \trans('Maximum 3 :model doivent être publiées en même temps, la plus ancienne est donc dépubliée.', [ 'model' => trans_choice('models.classes.news_club', \INF), ])); } }, ], 'publish_at' => 'sometimes|nullable|date', 'unpublish_at' => 'sometimes|nullable|date|after:publish_at', ]; return \array_merge( $rules, $this->pictureRules('picture', 400, 225, 400, 225), ); } /** * Get custom attributes for validator errors. */ public function attributes(): array { return [ 'type' => trans('validation.custom.type'), 'title' => trans('validation.attributes.title'), 'picture' => trans('validation.attributes.image'), 'picture_alt' => trans('validation.custom.attribute_alt'), 'picture_title' => trans('validation.custom.attribute_title'), 'multiple_answers' => trans('validation.custom.multiple_answers'), 'published' => trans('validation.attributes.published'), 'publish_at' => trans('validation.custom.publish_at'), 'unpublish_at' => trans('validation.custom.unpublish_at'), ]; } }