ldc-groupe/app/Http/Requests/Front/Search/SearchAjaxRequest.php

50 lines
1.6 KiB
PHP

<?php
namespace App\Http\Requests\Front\Search;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Http\FormRequest;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class SearchAjaxRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return $this->ajax();
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$supportedLocales = implode(',', LaravelLocalization::getSupportedLanguagesKeys());
return [
'search' => 'required|string|between:3,255',
'page' => 'required|integer|min:1',
'locale' => "required|string|in:$supportedLocales",
'class' => [
'required',
'string',
'starts_with:App\\Models\\',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, $value, $fail) {
if (!class_exists($value)) {
$fail("The specified model does not exist.");
return;
}
if (!is_subclass_of($value, Model::class)) {
$fail("The specified model is not an Eloquent model.");
}
},
],
];
}
}