84 lines
3.1 KiB
PHP
84 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Enums\RecipeDifficulty;
|
|
use App\Enums\RecipeType;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StoreRecipe extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function authorize()
|
|
{
|
|
$this->merge(['slug' => Str::slug($this->title)]);
|
|
$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,
|
|
[
|
|
'title' => 'required|min:10|max:100',
|
|
'slug' => 'required|unique:recipes,slug|min:1|max:255',
|
|
'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',
|
|
'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())
|
|
]));
|
|
}
|
|
}
|
|
]
|
|
]
|
|
);
|
|
}
|
|
}
|