route('general_setting')); } /** * Prepare the data for validation. */ 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::breederMap: $this->prepareBreederMapDatas(); break; case SettingsType::mainMenu: $this->prepareMainMenuDatas(); break; } } /** * Get the validation rules that apply to the request. * * @return array|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::breederMap: $rules = $this->getRulesBreederMap(); break; case SettingsType::mainMenu: $rules = $this->getRulesMainMenu(); break; } return $rules; } /** * Get custom attributes for validator errors. */ 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. */ private function prepareHotNewsDatas(): void { $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'), ]); $this->mergePicture('picture'); } /** * Prepare validation for Breeder Map. */ private function prepareBreederMapDatas(): 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. */ 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; if (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourCommitments)) { $element['link'] = (new Link(routeName: MenuNodeType::ourCommitments->routeName()))->toArray(); } elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::siteLogo)) { $element['link'] = (new Link(routeName: MenuNodeType::siteLogo->routeName()))->toArray(); } elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::breedings)) { $element['link'] = (new Link(routeName: MenuNodeType::breedings->routeName()))->toArray(); } elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourBrand)) { $element['link'] = (new Link(routeName: MenuNodeType::ourBrand->routeName()))->toArray(); } elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::news)) { $element['link'] = (new Link(routeName: MenuNodeType::news->routeName()))->toArray(); } elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourProducts)) { $element['link'] = (new Link(routeName: MenuNodeType::ourProducts->routeName()))->toArray(); } elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourRecipes)) { $element['link'] = (new Link(routeName: MenuNodeType::ourRecipes->routeName()))->toArray(); } elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::recruitment)) { $element['link'] = (new Link(routeName: MenuNodeType::recruitment->routeName()))->toArray(); } return $element; })->toArray(), ]); } /** * Get tha array of rules for the Main Menu. */ private function getRulesMainMenu(): array { /** @var array $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. */ 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', 500, 281, 500, 281), ); } /** * Get tha array of rules for the Breeder Map. */ private function getRulesBreederMap(): array { return [ 'title' => 'required|string|between:3,255', 'main_text' => ['required', 'string', new WysiwygLength(3)], 'picto_text' => 'required|string|between:3,255', ]; } }