check(); } /** * Prepare the data for validation. * * @return void * @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh, Squiz.Commenting.FunctionComment.Missing */ protected function prepareForValidation(): void { //phpcs:enable $this->merge([ 'slug' => Str::slug(strip_tags($this->input('title'))), 'published' => $this->boolean('published'), 'tags' => $this->input('tags', []), 'products' => $this->input('products', []), 'associated_tips_list' => $this->input('associated_tips_list', []), 'associated_recipes_list' => $this->input('associated_recipes_list', []), // * For sync relations 'tags_list' => \collect($this->input('tags'))->map(function ($value) { return \is_array($value) ? ($value['value'] ?? null) : null; })->filter()->all(), 'products_list' => \collect($this->input('products'))->map(function ($value) { return \is_array($value) ? ($value['value'] ?? null) : null; })->filter()->all(), 'tips_list' => \collect($this->input('tips'))->map(function ($value) { return \is_array($value) ? ($value['value'] ?? null) : null; })->filter()->all(), 'recipes_list' => \collect($this->input('recipes'))->map(function ($value) { return \is_array($value) ? ($value['value'] ?? null) : null; })->filter()->all(), ]); $this->mergePicture('picture'); $this->mergePicture('sticker'); $this->mergePicture('sticker_sm'); } /** * Get the validation rules that apply to the request. * * @return array * @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh, Squiz.Commenting.FunctionComment.Missing */ public function rules(): array { //phpcs:enable $rules = [ 'title' => 'required|min:3|max:255', 'seo_title' => 'required|min:45|max:70', 'seo_description' => 'required|min:50|max:160', 'type' => ['required', new Enum(AdviceTypeEnum::class)], 'picture_alt' => 'required_with:picture|nullable|min:3|max:255', 'picture_title' => 'required_with:picture|nullable|min:3|max:255', 'sticker_alt' => 'required_with:sticker|nullable|min:3|max:255', 'sticker_title' => 'required_with:sticker|nullable|min:3|max:255', 'sticker_sm_alt' => 'required_with:sticker_sm|nullable|min:3|max:255', 'sticker_sm_title' => 'required_with:sticker_sm|nullable|min:3|max:255', 'preparation' => [ 'required', 'string', new WysiwygLength(15, 2500) ], 'advice' => [ 'nullable', 'string', new WysiwygLength(15, 2500) ], 'ingredients' => [ 'present', 'array', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, array $value, $fail) { if (count($value) < 1 || count($value) > 15) { $fail(\trans('Vous devez associer 1 à 15 lignes d\'ingrédients.')); } } ], 'ingredients.*' => 'required|array', 'ingredients.*.value' => 'nullable|regex:/^\d*\.?\d{0,1}$/|between:0,65535', 'ingredients.*.label' => [ Rule::requiredIf(function () { return $this->type === AdviceTypeEnum::recipes->value(); }), //phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, $value, $fail) { if ($this->type == AdviceTypeEnum::recipes->value()) { if (!$value) { $fail(\trans('Le champ description de l\'ingrédient doit être une chaîne de caractères.')); } if (strlen($value) < 3) { $fail(\trans('Le texte description de l\'ingrédient doit contenir au moins 3 caractères.')); } } }, 'max:255' ], 'guests_nb' => [ //phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, $value, $fail) { if ( \is_array($this->ingredients) and count($this->ingredients) and !$value ) { $fail(\trans('Le nombre de convives est obligatoire')); } }, 'nullable', 'numeric', 'min:1', 'max:99' ], 'youtube_title' => 'required_with:youtube_video_id|nullable|min:3|max:255', 'youtube_video_id' => 'required_with:youtube_title|nullable|min:3|max:50|regex:%([^"&?\/\s]{11})%i', 'cooking_time' => 'nullable|numeric|min:0|max:65535', 'preparation_time' => [ Rule::requiredIf(function () { return $this->type === AdviceTypeEnum::recipes->value(); }), 'numeric', 'min:0', 'max:65535' ], 'difficulty' => [ Rule::requiredIf(function () { return $this->type === AdviceTypeEnum::recipes->value(); }), new Enum(AdviceDifficultyEnum::class) ], 'published' => 'required|boolean', 'tags' => [ 'present', 'array', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, array $value, $fail) { if (count($value) > 2) { $fail(\trans('Vous devez associer au maximum 2 tags.')); } } ], 'tags.*' => 'required|array', 'tags.*.id' => 'required|numeric|exists:tags,id|distinct', 'tags.*.label' => 'required|string', 'products' => [ 'present', 'array', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, array $value, $fail) { if ($this->type == AdviceTypeEnum::recipes->value()) { if (count($value) < 1 || count($value) > 3) { $fail(\trans('Vous devez associer 1 à 3 produits.')); } } else { if (count($value) > 3) { $fail(\trans('Vous devez associer au maximum 3 produits.')); } } } ], 'products.*' => 'required|array', 'products.*.value' => 'required|exists:products,id', 'products.*.label' => 'required|string', 'associated_tips_list' => [ 'present', 'array', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, array $value, $fail) { if (count($value) > 3) { $fail(\trans('Vous devez associer au maximum 3 astuces.')); } } ], 'associated_tips_list.*' => 'required|array', 'associated_tips_list.*.value' => 'required|exists:advice,id', 'associated_tips_list.*.label' => 'required|string', 'associated_recipes_list' => [ 'present', 'array', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, array $value, $fail) { if (count($value) > 3) { $fail(\trans('Vous devez associer au maximum 3 recettes.')); } } ], 'associated_recipes_list.*' => 'required|array', 'associated_recipes_list.*.value' => 'required|exists:advice,id', 'associated_recipes_list.*.label' => 'required|string', ]; return \array_merge( $rules, $this->pictureRules('picture', 650, 690, 650, 690, false), $this->pictureRules('sticker', 370, 300, 370, 300, false), $this->pictureRules('sticker_sm', 270, 145, 270, 145, false), ); } /** * Get custom attributes for validator errors. * * @return array */ public function attributes(): array { return [ 'title' => trans('Titre'), 'slug' => trans('Slug'), 'seo_title' => trans('title') . ' seo', 'seo_description' => trans('description') . ' seo', 'tags' => trans('tag'), 'tags.*' => trans('tag'), 'tags.*.id' => trans('id du tag'), 'tags.*.label' => trans('label du tag'), 'difficulty' => trans('Difficulté'), 'picture' => trans('Visuel'), 'picture_alt' => trans('Attribut alt du visuel'), 'picture_title' => trans('Titre du visuel'), 'sticker' => trans('Vignette'), 'sticker_alt' => trans('Attribut alt de la vignette'), 'sticker_title' => trans('Titre de la vignette'), 'sticker_sm' => trans('Petite vignette'), 'sticker_sm_alt' => trans('Attribut alt de la petite vignette'), 'sticker_sm_title' => trans('Titre de la petite vignette'), 'ingredients.*.value' => trans('quantité de l\'ingrédient'), 'ingredients.*.label' => trans('description de l\'ingrédient'), 'guests_nb' => trans('Nombre de convives'), 'youtube_title' => trans('Titre de la vidéo Youtube'), 'youtube_video_id' => trans('ID de la vidéo Youtube'), 'advice' => trans('Conseil LGP'), ]; } }