ldc-nature-eleveurs/app/Http/Requests/Bo/UpdateMiniCrudRequest.php

217 lines
8.7 KiB
PHP
Raw Permalink Normal View History

2022-08-09 12:29:21 +00:00
<?php
namespace App\Http\Requests\Bo;
use App\Enums\InputType;
2024-01-31 16:42:01 +01:00
use App\Lib\Helpers\ToolboxHelper;
2022-08-09 12:29:21 +00:00
use Illuminate\Foundation\Http\FormRequest;
2024-01-31 16:42:01 +01:00
use Illuminate\Validation\ValidationException;
2022-08-09 12:29:21 +00:00
abstract class UpdateMiniCrudRequest extends FormRequest
{
/**
2024-02-01 10:19:11 +01:00
* The model parameter name.
2022-08-09 12:29:21 +00:00
*
* @var string
*/
protected $modelName = null;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
2024-02-01 10:19:11 +01:00
public function authorize()
2022-08-09 12:29:21 +00:00
{
return auth('backend')->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
* @phpcs:disable Generic.Metrics.NestingLevel.TooHigh
2024-02-01 10:19:11 +01:00
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
2022-08-09 12:29:21 +00:00
*/
2024-02-01 10:19:11 +01:00
public function rules()
2022-08-09 12:29:21 +00:00
{
// phpcs:enable
$rules = [];
2024-02-01 10:19:11 +01:00
$data = $this->{$this->modelName}->data ?? null;
foreach ($data as $iptName => $input) {
2022-08-09 12:29:21 +00:00
$input = \json_decode(\json_encode($input), true);
2024-02-01 10:19:11 +01:00
switch ($input['inputType']['value']) {
2024-01-31 16:42:01 +01:00
case InputType::number->value:
$rules = array_merge($rules, [
"data.$iptName.value" => $this->compileValidate('required|numeric', $input)
]);
break;
case InputType::string->value:
2022-08-09 12:29:21 +00:00
$rules = array_merge($rules, [
"data.$iptName.value" => $this->compileValidate('required|string|min:3', $input)
]);
break;
2024-01-31 16:42:01 +01:00
case InputType::text->value:
2022-08-09 12:29:21 +00:00
$mergeRules = $this->compileValidate('required|string|min:15', $input);
2024-01-25 10:38:35 +01:00
$mergeRules = collect($mergeRules)
2022-08-09 12:29:21 +00:00
->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;
2024-02-01 10:19:11 +01:00
})->all();
2022-08-09 12:29:21 +00:00
$rules = array_merge($rules, [
2024-01-25 10:38:35 +01:00
"data.$iptName.value" => $mergeRules
2022-08-09 12:29:21 +00:00
]);
break;
2024-01-31 16:42:01 +01:00
case InputType::image->value:
2024-01-25 10:38:35 +01:00
$required = $this->isUpdate() ? 'sometimes' : 'required';
2024-02-01 10:19:11 +01:00
$ipt = $this->data[$iptName];
$max = isset($ipt['dimensionsMax']);
$dim = \is_string($input['dimensions']) ? collect(explode(
2022-08-09 12:29:21 +00:00
',',
2024-01-25 10:38:35 +01:00
(isset($ipt['dimensionsMax']) and is_string($ipt['dimensionsMax'])) ?
$ipt['dimensionsMax'] : $input['dimensions']
2022-08-09 12:29:21 +00:00
))->mapWithKeys(function ($v) {
$v = explode(' ', $v);
return [$v[0] => $v[1]];
2024-01-25 10:38:35 +01:00
}) : $input['dimensions'];
// * Adapt dimensions
$sizes = (object)[
'minHeight' => $dim['height'],
'maxHeight' => $dim['height'],
2024-02-01 10:19:11 +01:00
'minWidth' => $dim['width'],
'maxWidth' => $dim['width'],
2024-01-25 10:38:35 +01:00
];
2022-08-09 12:29:21 +00:00
$rules = array_merge($rules, [
2024-02-01 10:19:11 +01:00
"data.$iptName.value" => $this->compileValidate(
$required,
$input
),
2022-08-09 12:29:21 +00:00
"data.$iptName.value" => [
2024-01-25 10:38:35 +01:00
function (string $attribute, $value, callable $fail) use ($sizes) {
2024-02-01 10:19:11 +01:00
if ($value) {
ToolboxHelper::validatePicture($attribute, $value, $fail, $sizes);
}
2022-08-09 12:29:21 +00:00
}
2024-01-25 10:38:35 +01:00
],
2024-01-31 16:42:01 +01:00
"data.$iptName.alt" => $this->compileValidate("$required|string|min:5", $input),
2024-01-25 10:38:35 +01:00
"data.$iptName.title" => $this->compileValidate("$required|string|min:5", $input)
2022-08-09 12:29:21 +00:00
]);
break;
2024-01-31 16:42:01 +01:00
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:
$all = $this->all();
$nested = ToolboxHelper::dotStringToNestedArray(
"data.$iptName.value",
$this->input("data.$iptName.value") ? true : false
);
$all = ToolboxHelper::arrayMergeRecursiveDistinct($all, $nested);
$this->merge($all);
$rules = array_merge($rules, [
"data.$iptName.value" => 'boolean'
]);
break;
2022-08-09 12:29:21 +00:00
} //end switch
} //end foreach
return $rules;
}
/**
* Undocumented function
*
2024-01-25 10:38:35 +01:00
* @param string|array $inputTypeValidate
* @param array $input
* @return array
2024-02-01 10:19:11 +01:00
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
2022-08-09 12:29:21 +00:00
*/
2024-01-31 16:42:01 +01:00
public function compileValidate(string|array $inputTypeValidate, array $input): array
2022-08-09 12:29:21 +00:00
{
2024-02-01 10:19:11 +01:00
// phpcs:enable
2024-01-25 10:38:35 +01:00
if (\is_string($inputTypeValidate)) {
$inputTypeValidate = explode('|', $inputTypeValidate);
2022-08-09 12:29:21 +00:00
}
2024-01-25 10:38:35 +01:00
/** @var array|string */
$inputValidate = \collect($input)->get('validate', $input);
$inputValidate = \is_string($inputValidate) ? explode('|', $inputValidate) : $inputValidate;
2022-08-09 12:29:21 +00:00
foreach ($inputValidate as $validate) {
2024-01-25 10:38:35 +01:00
if (!\in_array($validate, $inputTypeValidate)) {
$inputTypeValidate[] = $validate;
2022-08-09 12:29:21 +00:00
}
}
2024-01-25 10:38:35 +01:00
2024-02-01 10:19:11 +01:00
$rules = \collect($inputTypeValidate)->unique()->sort(function ($valueA, $valueB) {
$valueA = $this->ruleHasToComeFirst($valueA);
$valueB = $this->ruleHasToComeFirst($valueB);
2024-01-25 10:38:35 +01:00
return $valueA - $valueB;
2024-02-01 10:19:11 +01:00
});
2024-01-25 10:38:35 +01:00
2024-02-01 10:19:11 +01:00
$hasRequire = $rules->first(fn ($rule) => $rule === 'required') ? true : false;
$hasRequireCond = $rules->first(fn ($rule) => \strpos($rule, 'required_') !== false) ? true : false;
$hasNullable = $rules->first(fn ($rule) => \strpos($rule, 'nullable') !== false) ? true : false;
$hasSometime = $rules->first(fn ($rule) => \strpos($rule, 'sometimes') !== false) ? true : false;
$hasPresent = $rules->first(fn ($rule) => \strpos($rule, 'present') !== false) ? true : false;
if ($hasRequire and ($hasRequireCond or $hasNullable or $hasSometime or $hasPresent)) {
$rules = $rules->reject(fn ($rule) => $rule === 'required');
}
return $rules->all();
2024-01-25 10:38:35 +01:00
}
/**
2024-02-01 10:19:11 +01:00
* Does the rule need to come first ?
2024-01-25 10:38:35 +01:00
*
* @param string $rule
2024-02-01 10:19:11 +01:00
* @return integer
2024-01-25 10:38:35 +01:00
*/
2024-02-01 10:19:11 +01:00
private function ruleHasToComeFirst(string $rule): int
2024-01-25 10:38:35 +01:00
{
2024-02-01 10:19:11 +01:00
$rule = \collect(\explode(':', $rule, 2))->filter()->first(null, '');
$rules = \collect([
'required_if',
2024-01-25 10:38:35 +01:00
'required_unless', 'required_with', 'required_with_all',
'required_without', 'required_without_all', 'required_with_all',
'missing', 'missing_if', 'missing_unless',
'missing_with', 'missing_with_all',
2024-02-01 10:19:11 +01:00
'nullable', 'sometimes', 'present', 'required'
]);
$itemKey = $rules->search(fn (string $firstRule) => \strpos($rule, $firstRule) === 0);
if (\is_bool($itemKey)) {
return $rules->count() + 1;
}
return $itemKey;
2024-01-25 10:38:35 +01:00
}
/**
* Is this request an update request ?
*
* @return boolean
*/
private function isUpdate(): bool
{
return $this->isMethod('PUT');
2022-08-09 12:29:21 +00:00
}
}