ldc-legaulois/app/Http/Requests/UpdateMiniCrudRequest.php
2022-02-10 16:14:58 +01:00

140 lines
5.3 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Enums\InputType;
use App\Lib\Utils;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
abstract class UpdateMiniCrudRequest extends FormRequest
{
/** The model parameter name */
protected $modelName = null;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [];
foreach ($this->{$this->modelName}->data as $iptName => $input) {
switch ($input['inputType']['value']) {
case InputType::string()->value:
$rules = array_merge($rules, [
"data.$iptName.value" => $this->compileValidate('required|string|min:4', $input)
]);
break;
case InputType::text()->value:
$mergeRules = $this->compileValidate('required|string|min:15', $input);
$mergeRules = collect(\explode('|', $mergeRules))
->map(function ($rule) {
if (\strpos($rule, 'max:') !== false) {
$max = \intval(str_replace('max:', '', $rule));
return function ($attribute, $value, $fail) use ($max) {
if (\strlen(\strip_tags($value)) > $max) {
$fail("$attribute ne doit pas excéder $max caractères");
}
};
}
return $rule;
});
$rules = array_merge($rules, [
"data.$iptName.text" => $mergeRules
]);
break;
case InputType::image()->value:
$ipt = $this->data[$iptName];
$max = isset($ipt['dimensionsMax']);
$dim = collect(explode(
',',
$ipt['dimensionsMax'] ?? $input['dimensions']
))->mapWithKeys(function ($v) {
$v = explode(' ', $v);
return [$v[0] => $v[1]];
});
$rules = array_merge($rules, [
"data.$iptName.value" => $this->compileValidate(
implode('|', [
'sometimes',
'image',
'mimes:png,jpg,jpeg',
sprintf(
'dimensions:%s=%d,%s=%d',
$max ? 'max_height' : 'height',
$dim['hauteur'],
$max ? 'max_width' : 'width',
$dim['largeur']
)
]),
$input
)
]);
break;
case InputType::url()->value:
$rules = array_merge($rules, [
"data.$iptName.value" => [
'required', 'url',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
try {
Utils::assertNoInternalUrls($attribute, $value);
} catch (ValidationException $e) {
$fail(collect($e->errors())->first()[0]);
}
}
]
]);
break;
case InputType::boolean()->value:
$all = $this->all();
$nested = Utils::dotStringToNestedArray(
"data.$iptName.value",
$this->input("data.$iptName.value") ? true : false
);
$all = array_merge_recursive_distinct($all, $nested);
$this->merge($all);
$rules = array_merge($rules, [
"data.$iptName.value" => 'boolean'
]);
break;
}//end switch
}//end foreach
return $rules;
}
/**
* Undocumented function
*
* @param string $inputTypeValidate
* @param string $configValidate
* @return string
*/
public function compileValidate(string $inputTypeValidate, array $input): string
{
if (!isset($input['validate']) or !\is_string($input['validate'])) {
return $inputTypeValidate;
}
/** @var array */
$inputValidate = explode('|', $input['validate']);
$out = explode('|', $inputTypeValidate);
foreach ($inputValidate as $validate) {
if (!\in_array($validate, $out)) {
$out[] = $validate;
}
}
return \implode('|', $out);
}
}