ldc-le-gaulois-pro/app/Http/Requests/Bo/Advice/StoreAdviceRequest.php
2023-01-18 11:29:15 +01:00

230 lines
10 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Advice;
use App\Enums\Advices\AdviceDifficultyEnum;
use App\Enums\Advices\AdviceTypeEnum;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
class StoreAdviceRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
// phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh, Squiz.Commenting.FunctionComment.Missing
protected function prepareForValidation()
{
$this->merge([
'slug' => Str::slug(strip_tags($this->title)),
'status' => $this->status ? true : false,
'tags' => $this->tags ?? [],
'products' => $this->products ?? [],
'associated_tips_list' => $this->associated_tips_list ?? [],
'associated_recipes_list' => $this->associated_recipes_list ?? [],
// * For sync relations
'tags_list' => \collect($this->tags)->map(function ($value) {
return \is_array($value) ? ($value['value'] ?? null) : null;
})->filter()->all(),
'products_list' => \collect($this->products)->map(function ($value) {
return \is_array($value) ? ($value['value'] ?? null) : null;
})->filter()->all(),
'tips_list' => \collect($this->associated_tips_list)->map(function ($value) {
return \is_array($value) ? ($value['value'] ?? null) : null;
})->filter()->all(),
'recipes_list' => \collect($this->associated_recipes_list)->map(function ($value) {
return \is_array($value) ? ($value['value'] ?? null) : null;
})->filter()->all(),
]);
$this->mergePicture('picture');
$this->mergePicture('sticker');
$this->mergePicture('sticker_sm');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
// phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh, Squiz.Commenting.FunctionComment.Missing
public function rules()
{
$rules = [
'title' => 'required|min:3|max:255',
'seo_title' => 'required|min:45|max:70',
'seo_description' => 'required|min:50|max:160',
'type' => ['required', new EnumIsValidRule(AdviceTypeEnum::class)],
'picture_alt' => 'required_with:picture|nullable|min:3|max:255',
'picture_title' => 'required_with:picture|nullable|min:3|max:255',
'sticker_alt' => 'required_with:sticker|nullable|min:3|max:255',
'sticker_title' => 'required_with:sticker|nullable|min:3|max:255',
'sticker_sm_alt' => 'required_with:sticker_sm|nullable|min:3|max:255',
'sticker_sm_title' => 'required_with:sticker_sm|nullable|min:3|max:255',
'preparation' => 'required|string|min:15',
'advice' => 'nullable|string|min:15',
'ingredients' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) {
if (count($value) < 1 || count($value) > 15) {
$fail(\trans('Vous devez associer 1 à 15 lignes d\'ingrédients.'));
}
}
],
'ingredients.*' => 'required|array',
'ingredients.*.value' => 'nullable|numeric|max:65535',
'ingredients.*.label' => [
Rule::requiredIf(function () {
return $this->type === AdviceTypeEnum::recipes()->value;
}),
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if ($this->type == AdviceTypeEnum::recipes()->value) {
if (!$value) {
$fail(\trans('Le champ description de l\'ingrédient doit être une chaîne de caractères.'));
}
if (strlen($value) < 3) {
$fail(\trans('Le texte description de l\'ingrédient doit contenir au moins 3 caractères.'));
}
}
},
'max:255'
],
'guests_nb' => [
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (
\is_array($this->ingredients) and
count($this->ingredients) and
!$value
) {
$fail(\trans('Le nombre de convives est obligatoire'));
}
}, 'nullable', 'numeric', 'min:1', 'max:99'
],
'youtube_title' => 'required_with:youtube_video_id|nullable|min:3|max:255',
'youtube_video_id' => 'required_with:youtube_title|nullable|regex:%([^"&?\/\s]{11})%i',
'cooking_time' => 'nullable|numeric|min:0|max:65535',
'preparation_time' => [
Rule::requiredIf(function () {
return $this->type === AdviceTypeEnum::recipes()->value;
}),
'numeric', 'min:0', 'max:65535'
],
'difficulty' => [
Rule::requiredIf(function () {
return $this->type === AdviceTypeEnum::recipes()->value;
}),
new EnumIsValidRule(AdviceDifficultyEnum::class)
],
'status' => 'required|boolean',
'tags' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) {
if (count($value) > 2) {
$fail(\trans('Vous devez associer au maximum 2 tags.'));
}
}
],
'tags.*' => 'required|array',
'tags.*.id' => 'required|numeric|exists:tags,id|distinct',
'tags.*.label' => 'required|string',
'products' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) {
if ($this->type == AdviceTypeEnum::recipes()->value) {
if (count($value) < 1 || count($value) > 3) {
$fail(\trans('Vous devez associer 1 à 3 produits.'));
}
} else {
if (count($value) > 3) {
$fail(\trans('Vous devez associer au maximum 3 produits.'));
}
}
}
],
'products.*' => 'required|array',
'products.*.value' => 'required|exists:products,id',
'products.*.label' => 'required|string',
'associated_tips_list' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) {
if (count($value) > 3) {
$fail(\trans('Vous devez associer au maximum 3 astuces.'));
}
}
],
'associated_tips_list.*' => 'required|array',
'associated_tips_list.*.value' => 'required|exists:advice,id',
'associated_tips_list.*.label' => 'required|string',
'associated_recipes_list' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) {
if (count($value) > 3) {
$fail(\trans('Vous devez associer au maximum 3 recettes.'));
}
}
],
'associated_recipes_list.*' => 'required|array',
'associated_recipes_list.*.value' => 'required|exists:advice,id',
'associated_recipes_list.*.label' => 'required|string',
];
return \array_merge(
$rules,
$this->pictureRules('picture', true, 650, 690, 650, 690),
$this->pictureRules('sticker', true, 370, 300, 370, 300),
$this->pictureRules('sticker_sm', true, 270, 145, 270, 145),
);
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'title' => trans('Titre'),
'slug' => trans('Slug'),
'seo_title' => trans('title') . ' seo',
'seo_description' => trans('description') . ' seo',
'tags' => trans('tag'),
'tags.*' => trans('tag'),
'tags.*.id' => trans('id du tag'),
'tags.*.label' => trans('label du tag'),
'difficulty' => trans('Difficulté'),
'picture' => trans('Visuel'),
'picture_alt' => trans('Attribut alt du visuel'),
'picture_title' => trans('Titre du visuel'),
'sticker' => trans('Vignette'),
'sticker_alt' => trans('Attribut alt de la vignette'),
'sticker_title' => trans('Titre de la vignette'),
'sticker_sm' => trans('Petite vignette'),
'sticker_sm_alt' => trans('Attribut alt de la petite vignette'),
'sticker_sm_title' => trans('Titre de la petite vignette'),
'ingredients.*.value' => trans('quantité de l\'ingrédient'),
'ingredients.*.label' => trans('description de l\'ingrédient'),
'guests_nb' => trans('Nombre de convives'),
'youtube_title' => trans('Titre de la vidéo Youtube'),
'youtube_video_id' => trans('ID de la vidéo Youtube'),
'advice' => trans('Conseil LGP'),
];
}
}