180 lines
7.4 KiB
PHP
180 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo;
|
|
|
|
use App\Enums\InputType;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
abstract class UpdateMiniCrudRequest extends FormRequest
|
|
{
|
|
/**
|
|
* The model parameter name
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $modelName = null;
|
|
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return auth('backend')->check();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function prepareForValidation(): void
|
|
{
|
|
foreach ($this->{$this->modelName}->data as $iptName => $input) {
|
|
$input = \json_decode(\json_encode($input), true);
|
|
switch ($input['inputType']['value']) {
|
|
case InputType::boolean()->value:
|
|
$all = $this->all();
|
|
$nested = Arr::undot([
|
|
"data.$iptName.value" =>
|
|
$this->input("data.$iptName.value") ? true : false
|
|
]);
|
|
$all = array_merge_recursive_distinct($all, $nested);
|
|
$this->merge($all);
|
|
break;
|
|
} //end switch
|
|
} //end foreach
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
* @phpcs:disable Generic.Metrics.NestingLevel.TooHigh
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
// phpcs:enable
|
|
$rules = [];
|
|
foreach ($this->{$this->modelName}->data as $iptName => $input) {
|
|
$input = \json_decode(\json_encode($input), true);
|
|
switch ($input['inputType']['value']) {
|
|
case InputType::number()->value:
|
|
$rules = array_merge($rules, [
|
|
"data.$iptName.value" => $this->compileValidate('required|numeric', $input)
|
|
]);
|
|
break;
|
|
case InputType::string()->value:
|
|
case InputType::email()->value:
|
|
$rules = array_merge($rules, [
|
|
"data.$iptName.value" => $this->compileValidate('required|string|min:1', $input)
|
|
]);
|
|
break;
|
|
case InputType::text()->value:
|
|
$mergeRules = $this->compileValidate('required|string|min:10', $input);
|
|
$mergeRules = collect($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($ipt['dimensionsMax'] ?? $input['dimensions']);
|
|
$rules = array_merge($rules, [
|
|
"data.$iptName.value" => array_merge(
|
|
$this->compileValidate('sometimes', $input),
|
|
[function ($attribute, $value, $fail) use ($max, $dim) {
|
|
try {
|
|
if (\is_object($value)) {
|
|
Validator::make(
|
|
[$attribute => $value],
|
|
// Replace dots since laravel framework 8.76.
|
|
[ToolboxHelper::mbReplace('.', '\.', $attribute) => [
|
|
'file',
|
|
'mimes:png,jpg,jpeg',
|
|
sprintf(
|
|
'dimensions:%s=%d,%s=%d',
|
|
$max ? 'max_height' : 'height',
|
|
$dim['hauteur'],
|
|
$max ? 'max_width' : 'width',
|
|
$dim['largeur']
|
|
)
|
|
]]
|
|
)->validate();
|
|
} //end if
|
|
} catch (ValidationException $e) {
|
|
$fail(collect($e->errors())->first()[0]);
|
|
} //end try
|
|
}]
|
|
),
|
|
"data.$iptName.alt" => $this->compileValidate('required|string|min:5', $input),
|
|
"data.$iptName.title" => $this->compileValidate('required|string|min:5', $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 {
|
|
ToolboxHelper::assertNoInternalUrls($attribute, $value);
|
|
ToolboxHelper::assertNoInternalBoUrls($attribute, $value);
|
|
} catch (ValidationException $e) {
|
|
$fail(collect($e->errors())->first()[0]);
|
|
}
|
|
}
|
|
]
|
|
]);
|
|
break;
|
|
case InputType::boolean()->value:
|
|
$rules = array_merge($rules, [
|
|
"data.$iptName.value" => $this->compileValidate('boolean', $input)
|
|
]);
|
|
break;
|
|
} //end switch
|
|
} //end foreach
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @param string $inputTypeValidate
|
|
* @param array $input
|
|
* @return array
|
|
*/
|
|
public function compileValidate(string $inputTypeValidate, array $input): array
|
|
{
|
|
if (!isset($input['validate']) or !\is_string($input['validate'])) {
|
|
return explode('|', $inputTypeValidate);
|
|
}
|
|
/** @var array */
|
|
$inputValidate = explode('|', $input['validate']);
|
|
$out = explode('|', $inputTypeValidate);
|
|
foreach ($inputValidate as $validate) {
|
|
if (!\in_array($validate, $out)) {
|
|
$out[] = $validate;
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
}
|