ldc-legaulois/app/Http/Requests/StoreRecipe.php

85 lines
3.1 KiB
PHP
Raw Normal View History

2021-04-30 20:55:00 +02:00
<?php
namespace App\Http\Requests;
use App\Enums\RecipeDifficulty;
use App\Enums\RecipeType;
use Illuminate\Foundation\Http\FormRequest;
2021-05-01 09:23:18 +02:00
use Illuminate\Support\Str;
2021-04-30 20:55:00 +02:00
class StoreRecipe extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
2022-02-10 16:14:58 +01:00
* @return boolean
2021-04-30 20:55:00 +02:00
*/
public function authorize()
{
2021-05-01 09:23:18 +02:00
$this->merge(['slug' => Str::slug($this->title)]);
2021-04-30 20:55:00 +02:00
$this->merge(['status' => $this->status === null ? 0 : 1]);
$this->merge(['notification' => $this->notification === null ? 0 : 1]);
if (intval($this->type) !== RecipeType::video()->value) {
$this->merge(['video_link' => null]);
}
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [];
if ($this->video_link !== null) {
$rules['video_link'] = 'required|max:255';
}
return array_merge(
$rules,
[
2021-05-01 09:23:18 +02:00
'title' => 'required|min:10|max:100',
'slug' => 'required|unique:recipes,slug|min:1|max:255',
2021-04-30 20:55:00 +02:00
'seo_title' => 'required|max:255',
'seo_description' => 'required|max:255',
'catch_phrase' => 'max:100',
'tags' => 'required|max:255',
'ingredients' => 'required|max:4000',
'preparation' => 'required|max:4000',
'advice' => 'max:1000',
'keywords' => 'max:255',
'preparation_time' => 'required|numeric|gte:0',
'cooking_time' => 'required|numeric|gte:0',
'number_people' => 'required|numeric|gte:0',
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=683,height=434',
'consumption_times' => 'required|exists:consumption_times,id',
2021-04-30 20:55:00 +02:00
'notification' => 'required|boolean',
'status' => 'required|boolean',
'type' => [
'required',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, RecipeType::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', RecipeType::toLabels())
]));
}
}
],
'difficulty' => [
'required',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, RecipeDifficulty::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', RecipeDifficulty::toLabels())
]));
}
}
]
]
);
}
}