65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Fo;
|
|
|
|
use App\Enums\CookingModeEnum;
|
|
use App\Enums\DifficultyEnum;
|
|
use App\Enums\PreparationTimeEnum;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class RecipesIndexRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'cooking_mode_enum' => $this->makeEnum(CookingModeEnum::class, $this->get('cooking_mode_enum')),
|
|
'preparation_time_enum' => $this->makeEnum(PreparationTimeEnum::class, $this->get('preparation_time_enum')),
|
|
'difficulty_enum' => $this->makeEnum(DifficultyEnum::class, $this->get('difficulty_enum'))
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'cooking_mode_enum' => ['nullable', Rule::enum(CookingModeEnum::class)],
|
|
'preparation_time_enum' => ['nullable', Rule::enum(PreparationTimeEnum::class)],
|
|
'difficulty_enum' => ['nullable', Rule::enum(DifficultyEnum::class)]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Make enum handle nullable case.
|
|
*
|
|
* @param string $enum_class
|
|
* @param string|null $query_index
|
|
* @return string|integer|null
|
|
*/
|
|
private function makeEnum(string $enum_class, ?string $query_index): string|int|null
|
|
{
|
|
if (!$query_index) {
|
|
return null;
|
|
}
|
|
return optional(call_user_func([$enum_class, 'make'], $query_index))->value();
|
|
}
|
|
}
|