94 lines
4.1 KiB
PHP
94 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class LoadMediaRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return auth()->check();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'offset' => 'sometimes|numeric',
|
|
'limit' => 'sometimes|numeric|gt:0|lte:50',
|
|
'folder' => 'sometimes|numeric',
|
|
'filters' => 'sometimes|array',
|
|
'filters.*' => 'required|array',
|
|
'filters.*.legend' => 'required|string',
|
|
'filters.*.libelle' => 'required|string',
|
|
'filters.*.order' => 'sometimes|in:desc,asc',
|
|
'filters.*.type' => 'required|in:order,dossier_id,type_media_id,type_vue_id,etiquette_id,arcadia_id,search',
|
|
'filters.*.value' => [
|
|
'nullable', function ($attribute, $value, $fail) {
|
|
$index = intval(collect(\explode('.', $attribute))->get(1));
|
|
$filterType = \strval(collect(collect($this->filters)->get($index))->get('type'));
|
|
switch ($filterType) {
|
|
case 'type_vue_id':
|
|
$attribute = \str_replace('.', '_', $attribute);
|
|
Validator::validate([$attribute => $value], [
|
|
$attribute => 'required|array',
|
|
"$attribute.*" => 'required|array',
|
|
"$attribute.*.codeTypevue" => 'required|exists:type_vues,codeTypevue',
|
|
]);
|
|
return;
|
|
case 'type_media_id':
|
|
$attribute = \str_replace('.', '_', $attribute);
|
|
Validator::validate([$attribute => $value], [
|
|
$attribute => 'required|array',
|
|
"$attribute.*" => 'required|array',
|
|
"$attribute.*.codeTypemedia" => 'required|exists:type_media,codeTypemedia',
|
|
]);
|
|
return;
|
|
case 'etiquette_id':
|
|
$attribute = \str_replace('.', '_', $attribute);
|
|
Validator::validate([$attribute => $value], [
|
|
$attribute => 'required|array',
|
|
"$attribute.*" => 'required|array',
|
|
"$attribute.*.etiquette_id" => 'required|exists:etiquettes,etiquette_id',
|
|
]);
|
|
return;
|
|
case 'dossier_id':
|
|
$attribute = \str_replace('.', '_', $attribute);
|
|
if (\intval($value) === 0) {
|
|
return;
|
|
}
|
|
Validator::validate([$attribute => $value], [
|
|
$attribute => 'required|exists:dossiers,id'
|
|
]);
|
|
return;
|
|
case 'order':
|
|
if (!\in_array($value, ['created_at', 'updated_at', 'version', 'libelle.keyword'])) {
|
|
$fail("Unhandled filter type order for '$value'");
|
|
}
|
|
return;
|
|
case 'search':
|
|
$attribute = \str_replace('.', '_', $attribute);
|
|
Validator::validate([$attribute => $value], [
|
|
$attribute => 'required|string'
|
|
]);
|
|
return;
|
|
default:
|
|
$fail("Unhandled filter type '$filterType'");
|
|
return;
|
|
} //end switch
|
|
}
|
|
],
|
|
];
|
|
}
|
|
}
|