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

64 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Requests;
2021-04-29 10:23:46 +02:00
use App\Enums\RecipeFilter;
use App\Exceptions\RedirectTo;
use Illuminate\Foundation\Http\FormRequest;
class RecipeIndexRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
2021-04-29 10:23:46 +02:00
if ($this->type) {
$this->merge([
'type' => $this->type
]);
}
if ($this->value) {
$this->merge([
'value' => $this->value
]);
}
if ($this->search) {
$this->merge([
'search' => $this->search
]);
}
if ($this->video_recipe) {
$this->merge([
'video_recipe' => $this->video_recipe
]);
}
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
2021-04-26 10:29:34 +02:00
'type' => [
2021-04-29 10:23:46 +02:00
'sometimes',
2021-06-14 15:15:52 +02:00
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed,Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
2021-04-26 10:29:34 +02:00
function ($attribute, $value, $fail) {
2021-04-29 10:23:46 +02:00
if (!in_array($value, RecipeFilter::toValues())) {
throw new RedirectTo(route('recipes.index'));
2021-04-26 10:29:34 +02:00
}
}
2021-04-26 10:29:34 +02:00
],
'value' => 'sometimes',
'search' => 'sometimes',
'video_recipe' => 'sometimes'
];
}
}