ldc-loue/app/Http/Requests/Bo/Recipes/StoreRecipeRequest.php
Alexis Gousseau 4a78ce9369 sprint 4
2022-11-07 16:24:39 +01:00

105 lines
3.8 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Recipes;
use App\Enums\Recipes\RecipeCategoryEnum;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
/**
* Unicity for fields is handled by model events
*/
class StoreRecipeRequest 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
*/
protected function prepareForValidation()
{
$this->merge([
'slug' => Str::slug($this->title),
'status' => $this->status ? true : false
]);
$this->mergePicture('banner');
$this->mergePicture('picture');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return \array_merge(
[
'title' => 'required|min:6|max:255',
'slug' => 'required|max:255',
'seo_title' => 'required|min:53|max:60',
'seo_description' => 'required|min:50|max:160',
'banner_alt' => 'required_with:banner|nullable|string|min:6|max:255',
'banner_title' => 'required_with:banner|nullable|string|min:6|max:255',
'category' => ['required', new EnumIsValidRule(RecipeCategoryEnum::class)],
'servings' => 'required|numeric|min:0',
'preparation' => 'required|string|min:6|max:255',
'cooking' => 'required|string|min:6|max:255',
'ingredients' => 'required|array',
'ingredients.*.value' => 'required|nullable|numeric',
'ingredients.*.label' => 'required|nullable|string|max:255',
'description' => 'required|string|min:6|max:255',
'video' => 'required|string|min:6|max:255',
'picture_alt' => 'required_with:picture|nullable|string|min:6|max:255',
'picture_title' => 'required_with:picture|nullable|string|min:6|max:255',
'status' => 'required|boolean'
],
$this->pictureRules('banner', 695, 557, 695, 557),
$this->pictureRules('picture', 333, 182, 333, 182)
);
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'title' => trans('title'),
'slug' => trans('title'),
'seo_title' => trans('Title') . ' seo',
'seo_description' => trans('Description') . ' seo',
'banner' => trans('Bannière'),
'banner_alt' => trans('Texte alternatif de la bannière'),
'banner_title' => trans('Titre de la bannière'),
'category' => trans('Catégorie de la recette'),
'servings' => trans('Chiffre/nombre de portions de la recette'),
'preparation' => trans('Temps de préparation de la recette'),
'cooking' => trans('Temps de cuisson de la recette'),
'ingredients' => trans('Ingrédients contenus dans la recette'),
'ingredients.*.value' => trans('Quantité de l\'ingrédient contenus dans la recette'),
'ingredients.*.label' => trans('Libellé de l\'ingrédient contenus dans la recette'),
'description' => trans('Description de la recette'),
'picture' => trans('Image de la recette'),
'picture_alt' => trans('Texte alternatif de la recette'),
'picture_title' => trans('Titre de la recette'),
'status' => trans('Publié ?')
];
}
}