186 lines
8.1 KiB
PHP
186 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Advice;
|
|
|
|
use App\Enums\Advices\AdviceDifficultyEnum;
|
|
use App\Enums\Advices\AdviceTypeEnum;
|
|
use App\Traits\Requests\HasPicture;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
|
|
|
class StoreAdviceRequest extends FormRequest
|
|
{
|
|
use HasPicture;
|
|
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return auth('backend')->check();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
// phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh, Squiz.Commenting.FunctionComment.Missing
|
|
protected function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'slug' => Str::slug(strip_tags($this->title)),
|
|
'status' => $this->status ? true : false,
|
|
'tags' => $this->tags ?? [],
|
|
'products' => $this->products ?? [],
|
|
'associated_tips_list' => $this->associated_tips_list ?? [],
|
|
'associated_recipes_list' => $this->associated_recipes_list ?? [],
|
|
// * For sync relations
|
|
'tags_list' => \collect($this->tags)->map(function ($value) {
|
|
return \is_array($value) ? ($value['value'] ?? null) : null;
|
|
})->filter()->all(),
|
|
'products_list' => \collect($this->products)->map(function ($value) {
|
|
return \is_array($value) ? ($value['value'] ?? null) : null;
|
|
})->filter()->all(),
|
|
'tips_list' => \collect($this->associated_tips_list)->map(function ($value) {
|
|
return \is_array($value) ? ($value['value'] ?? null) : null;
|
|
})->filter()->all(),
|
|
'recipes_list' => \collect($this->associated_recipes_list)->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
|
|
*/
|
|
public function rules()
|
|
{
|
|
$rules = [
|
|
'title' => 'required|min:3|max:255',
|
|
'type' => ['required', new EnumIsValidRule(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|min:15',
|
|
'advice' => 'nullable|string|min:15',
|
|
'ingredients' => [
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
'present', 'array', 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|numeric|max:65535',
|
|
'ingredients.*.label' => 'required|string|min:3|max:255',
|
|
'guests_nb' => 'nullable|numeric|max:99',
|
|
'youtube_video_id' => 'nullable|regex:%([^"&?\/\s]{11})%i',
|
|
'cooking_time' => 'nullable|numeric|min:0|max:65535',
|
|
'preparation_time' => 'nullable|numeric|min:0|max:65535',
|
|
'difficulty' => [
|
|
'required_if:type,==,AdviceTypeEnum::recipes()->value',
|
|
new EnumIsValidRule(AdviceDifficultyEnum::class)
|
|
],
|
|
'status' => 'required|boolean',
|
|
'tags' => [
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
'present', 'array', 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' => [
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
'present', 'array', 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' => [
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
'present', 'array', 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' => [
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
'present', 'array', 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', true, 650, 690, 650, 690),
|
|
$this->pictureRules('sticker', true, 370, 300, 370, 300),
|
|
$this->pictureRules('sticker_sm', true, 270, 145, 270, 145),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'title' => trans('Titre'),
|
|
'slug' => trans('Slug'),
|
|
'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'),
|
|
'guests_nb' => trans('Nombre de convives'),
|
|
'advice' => trans('Conseil LGP'),
|
|
];
|
|
}
|
|
}
|