ldc-legaulois/app/Http/Requests/Bo/GeneralSettings/UpdateGeneralSettingRequest.php

149 lines
6 KiB
PHP

<?php
namespace App\Http\Requests\Bo\GeneralSettings;
use App\Enums\Settings\MenuNodeType;
use App\Enums\Settings\SettingsType;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\Rules\Enum;
class UpdateGeneralSettingRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return Gate::check('update', $this->route('general_setting'));
}
/**
* Prepare the data for validation.
*
* @return void
*/
public function prepareForValidation(): void
{
/** @var \App\Models\GeneralSetting $generalSettingModel */
$generalSettingModel = $this->route('general_setting');
if ($generalSettingModel->type === SettingsType::hotNews) {
$this->mergePicture('picture');
$this->merge([
'value->picture' => $this->file('picture'),
'value->text' => $this->input('text'),
'value->picture_alt' => $this->input('picture_alt'),
'value->picture_title' => $this->input('picture_title'),
]);
return;
}
$this->merge([
'value' => collect(json_decode($this->input('jsonValue', '[]'), true))->map(function ($element) {
$element['published'] = (array_key_exists('published', $element)) ? $element['published'] : false;
return $element;
})->toArray()
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
/** @var \App\Models\GeneralSetting $generalSettingModel */
$generalSettingModel = $this->route('general_setting');
if ($generalSettingModel->type === SettingsType::hotNews) {
$rules = [
'text' => 'required|string|between:3,255',
'picture_alt' => 'required|string|between:3,255',
'picture_title' => 'required|string|between:3,255',
];
return \array_merge(
$rules,
$this->pictureRules('picture', 200, 100, 200, 100, false),
);
} else {
/** @var array<array-key, mixed> $routesName Get all front routes name */
$routesName = collect(Route::getRoutes())->map(function ($route) {
if (preg_match('/fo.([a-zA-Z])+.([a-zA-Z])+/', $route->getName())) {
return $route->getName();
}
})->filter()->toArray();
/** @var int $count Count all node published */
$count = collect($this->value)->filter(function ($element) {
if ($element['published']) {
return $element;
}
})->count();
return [
'jsonValue' => 'required|json',
'value' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) use ($count) {
if ($count > 8) {
$fail(\trans('Maximum 8 liens doivent être présents dans le menu'));
}
}
],
'value.*.type' => ['required', new Enum(MenuNodeType::class)],
'value.*.published' => 'required|boolean',
'value.*.title' => 'required|string|between:3,100',
'value.*.link.url' => 'nullable|string|url:https',
'value.*.link.routeName' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
'nullable', 'string', function ($attribute, string $value, $fail) use ($routesName) {
if (!in_array($value, $routesName)) {
$fail(\trans('Doît être un lien front existant'));
}
}
],
];
} //end if
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'text' => trans('validation.attributes.text'),
'picture' => trans('validation.attributes.image'),
'picture_alt' => trans('validation.custom.attribute_alt'),
'picture_title' => trans('validation.custom.attribute_title'),
'value' => trans('validation.custom.value'),
'value.*.type' => trans(':field :internoeud', [
'field' => trans('validation.custom.type'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.published' => trans(':field :internoeud', [
'field' => trans('validation.custom.published'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.title' => trans(':field :internoeud', [
'field' => trans('validation.attributes.title'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.link.url' => trans(':field :internoeud', [
'field' => trans('validation.custom.url'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.link.routeName' => trans(':field :internoeud', [
'field' => trans('validation.custom.route_name'),
'inter' => trans('validation.custom.of_the.male'),
]),
];
}
}