2024-05-15 12:40:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Bo\Recipes;
|
|
|
|
|
|
|
|
|
|
use App\Enums\Recipes\DifficultyEnum;
|
|
|
|
|
use App\Models\Recipe;
|
|
|
|
|
use App\Traits\Requests\HasPicture;
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
|
use Illuminate\Support\Str;
|
2026-09-25 16:25:31 +02:00
|
|
|
use Illuminate\Validation\Rules\Enum;
|
2026-10-14 15:50:46 +02:00
|
|
|
use App\Rules\WysiwygLength;
|
2024-05-15 12:40:31 +02:00
|
|
|
|
|
|
|
|
class StoreRecipeRequest extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
use HasPicture;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*/
|
|
|
|
|
public function authorize(): bool
|
|
|
|
|
{
|
|
|
|
|
return Gate::check('create', Recipe::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*/
|
|
|
|
|
protected function prepareForValidation(): void
|
|
|
|
|
{
|
2024-05-16 14:06:45 +02:00
|
|
|
$arrayIngredientList = [];
|
2026-10-14 15:50:46 +02:00
|
|
|
foreach ($this->input('ingredients_list', []) as $key => $value) {
|
2024-05-16 14:06:45 +02:00
|
|
|
$value = array_merge(
|
|
|
|
|
$value,
|
|
|
|
|
['is_proportionnal' => filter_var($value['is_proportionnal'] ?? false, FILTER_VALIDATE_BOOLEAN)]
|
|
|
|
|
);
|
|
|
|
|
$arrayIngredientList = array_merge($arrayIngredientList, [$key => $value]);
|
|
|
|
|
}
|
2024-05-15 12:40:31 +02:00
|
|
|
$this->merge([
|
2024-05-16 14:06:45 +02:00
|
|
|
'highlight' => $this->boolean('highlight'),
|
|
|
|
|
'published' => $this->boolean('published'),
|
|
|
|
|
'ingredients_list' => $arrayIngredientList,
|
2026-10-14 15:50:46 +02:00
|
|
|
'consumption_moments' => collect($this->input('consumption_moments', []))
|
|
|
|
|
->pluck('id')
|
|
|
|
|
->filter()
|
|
|
|
|
->values()
|
|
|
|
|
->all(),
|
2024-05-15 12:40:31 +02:00
|
|
|
]);
|
|
|
|
|
$this->mergePicture('picture');
|
2026-10-14 15:50:46 +02:00
|
|
|
|
|
|
|
|
foreach (['advice_picto', 'tips_picto'] as $picto) {
|
|
|
|
|
$this->mergePicture($picto);
|
|
|
|
|
|
|
|
|
|
// icon removed: mergePicture() returns [] instead of null
|
|
|
|
|
if ($this->input($picto) === []) {
|
|
|
|
|
$this->merge([$picto => null]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-15 12:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
|
|
|
|
$rules = [
|
2024-07-12 11:39:15 +02:00
|
|
|
'seo_title' => 'required|min:45|max:70',
|
|
|
|
|
'seo_description' => 'required|min:50|max:160',
|
2024-05-16 14:06:45 +02:00
|
|
|
'title' => 'required|string|between:3,255',
|
|
|
|
|
'picture_alt' => 'required_with:picture|nullable|between:3,255',
|
|
|
|
|
'picture_title' => 'required_with:picture|nullable|between:3,255',
|
|
|
|
|
'ingredients_list.*' => 'required|array',
|
2024-09-19 08:44:37 +00:00
|
|
|
'ingredients_list.*.value' => 'sometimes|nullable|decimal:0,2|between:1,1000',
|
2024-05-16 14:06:45 +02:00
|
|
|
'ingredients_list.*.label_unit' => [
|
2024-09-18 13:08:34 +02:00
|
|
|
'required_with:ingredients_list.*.label_unit',
|
|
|
|
|
'sometimes',
|
|
|
|
|
'nullable',
|
|
|
|
|
'string',
|
2026-09-25 16:25:31 +02:00
|
|
|
'between:1,255',
|
2024-05-16 14:06:45 +02:00
|
|
|
],
|
|
|
|
|
'ingredients_list.*.label_value' => 'required|string|between:3,255',
|
|
|
|
|
'ingredients_list.*.is_proportionnal' => 'required|boolean',
|
|
|
|
|
'preparation' => 'required|string|min:15',
|
2026-10-14 15:50:46 +02:00
|
|
|
'preparation_title' => 'required|string|between:3,255',
|
2024-05-16 14:06:45 +02:00
|
|
|
'video_id' => 'sometimes|nullable|integer|exists:videos,id',
|
2026-10-14 15:50:46 +02:00
|
|
|
'consumption_moments' => 'required|array|min:1|max:4',
|
|
|
|
|
'consumption_moments.*' => 'required|integer|distinct|exists:consumption_moments,id',
|
2024-05-16 14:06:45 +02:00
|
|
|
'products' => 'required|array',
|
|
|
|
|
'products.*' => 'required|integer|exists:products,id',
|
|
|
|
|
'preparation_time' => 'required|numeric|between:1,1000',
|
2024-09-19 08:44:37 +00:00
|
|
|
'cooking_time' => 'nullable|numeric|between:0,1000',
|
2024-05-16 14:06:45 +02:00
|
|
|
'nb_guests' => 'required|numeric|between:1,50',
|
|
|
|
|
'difficulty' => ['required', new Enum(DifficultyEnum::class)],
|
|
|
|
|
'highlight' => 'required|boolean',
|
|
|
|
|
'published' => 'required|boolean',
|
2026-10-14 15:50:46 +02:00
|
|
|
'advice_title' => 'nullable|string|between:3,255',
|
|
|
|
|
'advice' => ['nullable', 'string', new WysiwygLength(min: 15, max: 500)],
|
|
|
|
|
'advice_picto_alt' => 'required_with:advice_picto|nullable|between:3,255',
|
|
|
|
|
'advice_picto_title' => 'required_with:advice_picto|nullable|between:3,255',
|
|
|
|
|
'tips_title' => 'nullable|string|between:3,255',
|
|
|
|
|
'tips' => ['nullable', 'string', new WysiwygLength(min: 15, max: 500)],
|
|
|
|
|
'tips_picto_alt' => 'required_with:tips_picto|nullable|between:3,255',
|
|
|
|
|
'tips_picto_title' => 'required_with:tips_picto|nullable|between:3,255',
|
2024-05-15 12:40:31 +02:00
|
|
|
];
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-05-15 12:40:31 +02:00
|
|
|
return \array_merge(
|
|
|
|
|
$rules,
|
|
|
|
|
$this->pictureRules('picture', 720, 405, 720, 405),
|
2026-10-14 15:50:46 +02:00
|
|
|
$this->pictureRules('advice_picto', 45, 45, 45, 45, nullable: true),
|
|
|
|
|
$this->pictureRules('tips_picto', 45, 45, 45, 45, nullable: true),
|
2024-05-15 12:40:31 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get custom attributes for validator errors.
|
|
|
|
|
*/
|
|
|
|
|
public function attributes(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2024-07-12 11:39:15 +02:00
|
|
|
'seo_title' => trans('validation.custom.seo_title'),
|
|
|
|
|
'seo_description' => trans('validation.custom.seo_description'),
|
2024-05-16 14:06:45 +02:00
|
|
|
'title' => trans('validation.attributes.title'),
|
|
|
|
|
'tips' => trans('validation.custom.tips'),
|
2026-10-14 15:50:46 +02:00
|
|
|
'tips_title' => trans('validation.custom.tips_title'),
|
|
|
|
|
'tips_picto' => trans('validation.custom.tips_picto'),
|
|
|
|
|
'tips_picto_alt' => trans('validation.custom.attribute_alt'),
|
|
|
|
|
'tips_picto_title' => trans('validation.custom.attribute_title'),
|
2024-05-16 14:06:45 +02:00
|
|
|
'picture' => trans('validation.attributes.image'),
|
|
|
|
|
'picture_alt' => trans('validation.custom.attribute_alt'),
|
|
|
|
|
'picture_title' => trans('validation.custom.attribute_title'),
|
|
|
|
|
'ingredients_list' => trans('validation.custom.ingredients_list'),
|
|
|
|
|
'ingredients_list.*.label_unit' => trans('validation.custom.ingredients_list'),
|
|
|
|
|
'ingredients_list.*.label_value' => trans('validation.custom.ingredients_list'),
|
|
|
|
|
'ingredients_list.*.is_proportionnal' => trans('validation.custom.ingredients_list'),
|
|
|
|
|
'preparation' => trans('validation.custom.preparation'),
|
2026-10-14 15:50:46 +02:00
|
|
|
'preparation_title' => trans('validation.custom.preparation_title'),
|
2024-05-16 14:06:45 +02:00
|
|
|
'advice' => trans('validation.custom.advice'),
|
2026-10-14 15:50:46 +02:00
|
|
|
'advice_title' => trans('validation.custom.advice_title'),
|
|
|
|
|
'advice_picto' => trans('validation.custom.advice_picto'),
|
|
|
|
|
'advice_picto_alt' => trans('validation.custom.attribute_alt'),
|
|
|
|
|
'advice_picto_title' => trans('validation.custom.attribute_title'),
|
2024-05-16 14:06:45 +02:00
|
|
|
'video_id' => trans('validation.custom.video_id'),
|
2026-10-14 15:50:46 +02:00
|
|
|
'consumption_moments' => trans_choice('models.classes.consumption_moment', 2),
|
|
|
|
|
'consumption_moments.*' => trans_choice('models.classes.consumption_moment', 1),
|
2024-05-16 14:06:45 +02:00
|
|
|
'products' => Str::plural(trans('models.classes.product')),
|
|
|
|
|
'preparation_time' => trans('validation.custom.preparation_time'),
|
|
|
|
|
'cooking_time' => trans('validation.custom.cooking_time'),
|
|
|
|
|
'nb_guests' => trans('validation.custom.nb_guests'),
|
|
|
|
|
'difficulty' => trans('validation.custom.difficulty'),
|
|
|
|
|
'highlight' => trans('validation.custom.highlight'),
|
|
|
|
|
'published' => trans('validation.custom.published'),
|
2024-05-15 12:40:31 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|