ldc-legaulois/app/Http/Requests/Bo/GeneralSettings/UpdateGeneralSettingRequest.php
2024-09-13 17:11:15 +02:00

241 lines
8.3 KiB
PHP

<?php
namespace App\Http\Requests\Bo\GeneralSettings;
use App\Enums\Settings\MenuNodeType;
use App\Enums\Settings\SettingsType;
use App\Rules\WysiwygLength;
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');
switch ($generalSettingModel->type) {
case SettingsType::hotNews:
$this->prepareHotNewsDatas();
break;
case SettingsType::breederMapComponent:
$this->prepareBreederMapComponentDatas();
break;
case SettingsType::mainMenu:
$this->prepareMainMenuDatas();
break;
}
}
/**
* 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');
$rules = [];
switch ($generalSettingModel->type) {
case SettingsType::hotNews:
$rules = $this->getRulesHotNews();
break;
case SettingsType::breederMapComponent:
$rules = $this->getRulesBreederMapComponent();
break;
case SettingsType::mainMenu:
$rules = $this->getRulesMainMenu();
break;
}
return $rules;
}
/**
* 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'),
'tips_title' => trans('validation.attributes.title'),
'recipes_title' => trans('validation.attributes.title'),
'picto_text' => trans('validation.custom.breeder_map_component.picto_text'),
'main_text' => trans('validation.custom.breeder_map_component.main_text'),
'subtitle' => trans('validation.custom.subtitle'),
'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'),
]),
];
}
/**
* Prepare validation for Hot News
*
* @return void
*/
private function prepareHotNewsDatas(): void
{
$this->mergePicture('picture');
$this->merge([
'value->text' => $this->input('text'),
'value->picture' => $this->file('picture'),
'value->picture_alt' => $this->input('picture_alt'),
'value->picture_title' => $this->input('picture_title'),
]);
}
/**
* Prepare validation for Breeder Map Component
*
* @return void
*/
private function prepareBreederMapComponentDatas(): void
{
$this->merge([
'value->title' => $this->input('title'),
'value->main_text' => $this->input('main_text'),
'value->picto_text' => $this->input('picto_text'),
]);
}
/**
* Prepare validation for Breeder Map Component
*
* @return void
*/
private function prepareMainMenuDatas(): void
{
$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 tha array of rules for the Main Menu.
*
* @return array
*/
private function getRulesMainMenu(): array
{
/** @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' => [
'present',
'array',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
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' => [
'nullable',
'string',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, string $value, $fail) use ($routesName) {
if (!in_array($value, $routesName)) {
$fail(\trans('Doît être un lien front existant'));
}
}
],
];
}
/**
* Get tha array of rules for the Hot News.
*
* @return array
*/
private function getRulesHotNews(): array
{
$rules = [
'text' => ['required', 'string', new WysiwygLength(3)],
'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),
);
}
/**
* Get tha array of rules for the Breeder Map Component.
*
* @return array
*/
private function getRulesBreederMapComponent(): array
{
return [
'title' => 'required|string|between:3,255',
'main_text' => ['required', 'string', new WysiwygLength(3)],
'picto_text' => 'required|string|between:3,255',
];
}
}