ldc-legaulois/app/Http/Requests/UpdateMiniAcfRequest.php

226 lines
7.2 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Enums\InputType;
use App\Lib\Utils;
use Exception;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
class UpdateMiniAcfRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
protected function genRules(array $content)
{
$rules = [];
foreach ($content as $i => $block) {
foreach ($block['content'] as $iptName => $input) {
$rules = array_merge($rules, $this->getRule(
compact('i', 'iptName'),
$input['inputType']['value'],
$content[$i]['content'][$iptName]
));
}
}
return $rules;
}
public function attributes()
{
return [
'content' => \trans('Contenu')
];
}
// PRIVATE FUNCTIONS
private function getRule(array $coords, $inputType, $content): array
{
\extract($coords);
$inputKey = "content.$i.content.$iptName";
switch ($inputType) {
case InputType::string()->value:
return $this->handleString($inputKey);
case InputType::text()->value:
return $this->handleText($inputKey);
case InputType::image()->value:
return $this->handleImage($inputKey, $content);
case InputType::url()->value:
return $this->handleUrl($inputKey);
case InputType::boolean()->value:
return $this->handleBoolean($inputKey);
case InputType::imageArray()->value:
return $this->handleImageArray($coords, $inputKey, $content);
case InputType::dropdownArray()->value:
return $this->handleDropdownArray($coords, $inputKey);
case InputType::labelArray()->value:
return $this->handleLabelArray($coords, $inputKey);
}
throw new Exception('Unknown input type');
}
private function handleImageArray(array $coords, string $inputKey, array $ipt): array
{
\extract($coords);
$rules = [];
$ipt = $all['content'][$i]['content'][$iptName];
$max = isset($ipt['dimensionsMax']);
$dim = collect(explode(
',',
$ipt['dimensions'] ?? $ipt['dimensionsMax']
))->mapWithKeys(function ($v) {
$v = explode(' ', $v);
return [$v[0] => $v[1]];
});
$rules = array_merge($rules, [
"$inputKey.value.*" => 'sometimes|array'
]);
if (is_array($this->input("$inputKey.value.*"))) {
$all = $this->all();
$images = $this->input("$inputKey.value");
if (is_array($images)) {
ksort($images);
$all['content'][$i]['content'][$iptName]['value'] = $images;
$this->merge($all);
}
foreach ($this->input("$inputKey.value.*") as $imageK => $image) {
if (!is_string($image)) {
$rules = array_merge($rules, [
"$inputKey.$imageK" => [
'image',
'mimes:png,jpg',
sprintf(
'dimensions:%s=%d,%s=%d',
$max ? 'max_height' : 'height',
$dim['hauteur'],
$max ? 'max_width' : 'width',
$dim['largeur']
)
]
]);
} else {
$rules = array_merge($rules, [
"$inputKey.value.$imageK" => 'string'
]);
}
}
}
return $rules;
}
private function handleLabelArray(array $coords, string $inputKey): array
{
\extract($coords);
$rules = [];
$all = $this->all();
foreach ($this->input("$inputKey.value") as $labelIndex => $label) {
if ($labelIndex === 'X') {
unset($all['content'][$i]['content'][$iptName]['value'][$labelIndex]);
continue;
}
$rules = array_merge($rules, [
"$inputKey.value.$labelIndex.*" => 'string'
]);
}
$this->merge($all);
return $rules;
}
private function handleDropdownArray(array $coords, string $inputKey): array
{
\extract($coords);
$rules = [];
$all = $this->all();
foreach ($this->input("$inputKey.value") as $dropdownIndex => $dropdown) {
if ($dropdownIndex === 'X') {
unset($all['content'][$i]['content'][$iptName]['value'][$dropdownIndex]);
continue;
}
$rules = array_merge($rules, [
"$inputKey.value.$dropdownIndex.*" => 'string'
]);
}
$this->merge($all);
return $rules;
}
private function handleBoolean(string $inputKey): array
{
$all = $this->all();
$nested = Utils::dotStringToNestedArray(
"$inputKey.value",
$this->input("$inputKey.value") ? true : false
);
$all = array_merge_recursive_distinct($all, $nested);
$this->merge($all);
return ["$inputKey.value" => 'boolean'];
}
private function handleUrl(string $inputKey): array
{
return [
"$inputKey.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]);
}
}
]
];
}
private function handleImage(string $inputKey, array $ipt): array
{
$max = isset($ipt['dimensionsMax']);
$dim = collect(explode(
',',
$ipt['dimensions'] ?? $ipt['dimensionsMax'] ?? ''
))->mapWithKeys(function ($v) {
$v = explode(' ', $v);
return [$v[0] => $v[1]];
});
return [
"$inputKey.value" => [
'sometimes',
'image',
'mimes:png,jpg',
sprintf(
'dimensions:%s=%d,%s=%d',
$max ? 'max_height' : 'height',
$dim['hauteur'],
$max ? 'max_width' : 'width',
$dim['largeur']
)
]
];
}
private function handleText(string $inputKey): array
{
return ["$inputKey.html" => 'required|string|min:15'];
}
private function handleString(string $inputKey): array
{
return ["$inputKey.value" => 'required|string|min:4'];
}
}