ldc-nature-and-respect/app/Http/Requests/Bo/Recipes/StoreRecipeRequest.php
2023-05-04 12:07:27 +02:00

112 lines
3.8 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Recipes;
use App\Enums\Recipes\CookingEnum;
use App\Enums\Recipes\DifficultyEnum;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
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(strip_tags($this->name)),
'status' => $this->status ? true : false,
]);
$this->mergePicture('picture');
$this->mergePicture('picture_thumbnail');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'seo_title' => 'required|min:3|max:70',
'seo_description' => 'required|min:50|max:160',
'name' => 'required|min:3|max:255',
'slug' => 'unique_translation:recipes',
'picture_alt' => 'required|min:3|max:255',
'picture_title' => 'required|min:3|max:255',
'picture_thumbnail_alt' => 'required|min:3|max:255',
'picture_thumbnail_title' => 'required|min:3|max:255',
'ingredients' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) {
if (1 > count($value)) {
$fail(\trans('You must fill in at least 1 line of ingredients.'));
}
}
],
'ingredients.*' => 'nullable|array',
'ingredients.*.header' => 'nullable|string|min:3|max:255',
'ingredients.*.value' => 'nullable|string|min:3|max:255',
'teaser' => 'required|min:50|max:160',
'steps' => 'required|string|min:15',
'difficulty' => [new Enum(DifficultyEnum::class)],
'preparation' => 'required|numeric|min:1|max:1440',
'duration' => 'required|numeric|min:1|max:1440',
'tips' => 'required|string|min:15',
'cooking' => [new Enum(CookingEnum::class)],
'range_id' => 'required|exists:ranges,id',
'status' => 'required|boolean'
];
return \array_merge(
$rules,
$this->pictureRules('picture', 1600, 900, 1600, 900, true),
$this->pictureRules('picture_thumbnail', 430, 242, 430, 242, true),
);
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'seo_title' => trans('title') . ' seo',
'seo_description' => trans('description') . ' seo',
'name' => trans('name'),
'slug' => trans('slug'),
'ingredients' => trans('ingredients'),
'ingredients.*.header' => trans('header in the list of ingredients'),
'ingredients.*.label' => trans('value of ingredients'),
'ingredients.*.value' => trans('value of ingredients'),
'teaser' => trans('teaser'),
'steps' => trans('steps'),
'difficulty' => trans('difficulty'),
'preparation' => trans('preparation'),
'duration' => trans('duration'),
'tips' => trans('tips'),
'cooking' => trans('cooking'),
'range_id' => trans('range'),
'status' => trans('publish')
];
}
}