126 lines
5.9 KiB
PHP
126 lines
5.9 KiB
PHP
<?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\Validation\Rules\Enum;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StoreRecipeRequest extends FormRequest
|
|
{
|
|
use HasPicture;
|
|
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Gate::check('create', Recipe::class);
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$arrayIngredientList = [];
|
|
foreach ($this->input('ingredients_list') as $key => $value) {
|
|
$value = array_merge(
|
|
$value,
|
|
['is_proportionnal' => filter_var($value['is_proportionnal'] ?? false, FILTER_VALIDATE_BOOLEAN)]
|
|
);
|
|
$arrayIngredientList = array_merge($arrayIngredientList, [$key => $value]);
|
|
}
|
|
$this->merge([
|
|
'highlight' => $this->boolean('highlight'),
|
|
'published' => $this->boolean('published'),
|
|
'ingredients_list' => $arrayIngredientList,
|
|
]);
|
|
$this->mergePicture('picture');
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'seo_title' => 'required|min:45|max:70',
|
|
'seo_description' => 'required|min:50|max:160',
|
|
'title' => 'required|string|between:3,255',
|
|
'tips' => 'nullable|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',
|
|
'ingredients_list.*.value' => 'sometimes|nullable|decimal:0,2|between:1,1000',
|
|
'ingredients_list.*.label_unit' => [
|
|
'required_with:ingredients_list.*.label_unit',
|
|
'sometimes',
|
|
'nullable',
|
|
'string',
|
|
'between:1,255'
|
|
],
|
|
'ingredients_list.*.label_value' => 'required|string|between:3,255',
|
|
'ingredients_list.*.is_proportionnal' => 'required|boolean',
|
|
'preparation' => 'required|string|min:15',
|
|
'advice' => 'nullable|string|min:15',
|
|
'video_id' => 'sometimes|nullable|integer|exists:videos,id',
|
|
'consumption_moment_id' => 'required|integer|exists:consumption_moments,id',
|
|
'products' => 'required|array',
|
|
'products.*' => 'required|integer|exists:products,id',
|
|
'preparation_time' => 'required|numeric|between:1,1000',
|
|
'cooking_time' => 'nullable|numeric|between:0,1000',
|
|
'nb_guests' => 'required|numeric|between:1,50',
|
|
'difficulty' => ['required', new Enum(DifficultyEnum::class)],
|
|
'highlight' => 'required|boolean',
|
|
'published' => 'required|boolean',
|
|
];
|
|
return \array_merge(
|
|
$rules,
|
|
$this->pictureRules('picture', 720, 405, 720, 405),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'seo_title' => trans('validation.custom.seo_title'),
|
|
'seo_description' => trans('validation.custom.seo_description'),
|
|
'title' => trans('validation.attributes.title'),
|
|
'tips' => trans('validation.custom.tips'),
|
|
'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'),
|
|
'advice' => trans('validation.custom.advice'),
|
|
'video_id' => trans('validation.custom.video_id'),
|
|
'consumption_moment_id' => trans('models.classes.consumption_moment'),
|
|
'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'),
|
|
];
|
|
}
|
|
}
|