ldc-le-gaulois-pro/app/Http/Requests/Bo/News/StoreNewsRequest.php

153 lines
6 KiB
PHP

<?php
namespace App\Http\Requests\Bo\News;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
class StoreNewsRequest 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($this->title),
'status' => $this->status ? true : false,
'categories' => $this->categories ?? [],
'products' => $this->products ?? [],
'associated_news_list' => $this->associated_news_list ?? [],
// * For sync relations
'news_list' => \collect($this->associated_news_list)->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(),
'categories_list' => \collect($this->categories)->map(function ($value) {
return \is_array($value) ? ($value['value'] ?? null) : null;
})->filter()->all()
]);
$this->mergePicture('picture');
$this->mergePicture('picture2');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'title' => 'required|min:3|max:255',
'subtitle' => 'required|min:3|max:255',
'seo_title' => 'required|min:45|max:70',
'seo_description' => 'required|min:50|max:160',
'picture_alt' => 'required_with:picture|nullable|min:3|max:255',
'picture_title' => 'required_with:picture|nullable|min:3|max:255',
'picture2_alt' => 'required_with:picture2|nullable|min:3|max:255',
'picture2_title' => 'required_with:picture2|nullable|min:3|max:255',
'content' => 'required_with:picture|nullable|string|min:15',
'content2' => 'required_with:picture2|nullable|string|min:15',
'event_start' => 'nullable|date_format:Y-m-d',
'event_end' => [
'required_with:event_start',
'after_or_equal:event_start',
'nullable',
'date_format:Y-m-d'
],
'event_text' => [
'required_with:event_start',
'nullable',
'min:3',
'max:255'
],
'status' => 'required|boolean',
'categories' => [
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
'present', 'array', function ($attribute, array $value, $fail) {
if (count($value) < 1 || count($value) > 2) {
$fail(\trans('Vous devez associer une actualité à 1 ou 2 catégories.'));
}
}
],
'categories.*' => 'required|array',
'categories.*.value' => 'required|exists:news_categories,id',
'categories.*.label' => 'required|string',
'products' => [
// 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 produits.'));
}
}
],
'products.*' => 'required|array',
'products.*.value' => 'required|exists:products,id',
'products.*.label' => 'required|string',
'associated_news_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 actualités.'));
}
}
],
'associated_news_list.*' => 'required|array',
'associated_news_list.*.value' => 'required|exists:news,id',
'associated_news_list.*.label' => 'required|string',
];
return \array_merge(
$rules,
$this->pictureRules('picture', true, 920, 1025, 920, 1025),
$this->pictureRules('picture2', false, 920, 1025, 920, 1025)
);
}
/**
* 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',
'subtitle' => trans('Sous-titre'),
'picture' => trans('Image'),
'picture_alt' => trans('Attribut alt de l\'image'),
'picture_title' => trans('Titre de l\'image'),
'picture2' => trans('Image 2'),
'picture2_alt' => trans('Attribut alt de l\'image 2'),
'picture2_title' => trans('Titre de l\'image 2'),
'content' => trans('Contenu'),
'content2' => trans('Contenu 2'),
'event_start' => trans('Date de début de l\'évènement'),
'event_end' => trans('Date de fin de l\'évènement'),
'event_text' => trans('Description de l\'évènement'),
'status' => trans('Status'),
];
}
}