129 lines
4.3 KiB
PHP
129 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Ranges;
|
|
|
|
use App\Enums\Ranges\RangeCategoryEnum;
|
|
use App\Models\Breeder;
|
|
use App\Models\Recipe;
|
|
use App\Traits\Requests\HasPicture;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
|
|
|
/**
|
|
* Unicity for fields is handled by model events
|
|
*/
|
|
class StoreRangeRequest 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
|
|
*/
|
|
protected function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'slug' => Str::slug($this->title),
|
|
'status' => $this->status ? true : false
|
|
]);
|
|
$this->mergePicture();
|
|
$this->mergePicture('picture_thumb');
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return \array_merge(
|
|
[
|
|
'title' => 'required|min:6|max:255',
|
|
'slug' => 'required|max:255',
|
|
'category' => ['required', new EnumIsValidRule(RangeCategoryEnum::class)],
|
|
'seo_title' => 'required|min:45|max:70',
|
|
'seo_description' => 'required|min:50|max:160',
|
|
'description' => 'required|string|min:15',
|
|
'argumentation' => 'required|string|min:15',
|
|
'picture_alt' => 'required_with:picture|nullable|string|min:6|max:255',
|
|
'picture_title' => 'required_with:picture|nullable|string|min:6|max:255',
|
|
'picture_thumb_alt' => 'required_with:picture_thumb|nullable|string|min:6|max:255',
|
|
'picture_thumb_title' => 'required_with:picture_thumb|nullable|string|min:6|max:255',
|
|
'publish_at' => 'nullable|date_format:Y-m-d\TH:i',
|
|
'unpublish_at' => 'nullable|date_format:Y-m-d\TH:i',
|
|
'status' => 'required|boolean',
|
|
'link1' => self::getUrlValidationRules(),
|
|
'link2' => self::getUrlValidationRules(),
|
|
],
|
|
$this->pictureRules('picture', 700, 700, 700, 700),
|
|
$this->pictureRules('picture_thumb', 300, 300, 300, 300)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'title' => trans('title'),
|
|
'slug' => trans('title'),
|
|
'seo_title' => trans('title') . ' seo',
|
|
'seo_description' => trans('description') . ' seo',
|
|
'description' => trans('description'),
|
|
'argumentation' => trans('argumentation'),
|
|
'picture' => trans('illustration'),
|
|
'picture_alt' => trans('Texte alternatif de l\'illustration'),
|
|
'picture_title' => trans('Titre de l\'illustration'),
|
|
'picture_thumb' => trans('vignette'),
|
|
'picture_thumb_alt' => trans('Texte alternatif de la vignette'),
|
|
'picture_thumb_title' => trans('Titre de la vignette'),
|
|
'status' => trans('publié ?'),
|
|
'publish_at' => trans('date de publication'),
|
|
'unpublish_at' => trans('date de dépublication'),
|
|
'link1' => trans('lien 1'),
|
|
'link2' => trans('lien 2'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Url validation rules
|
|
* Validates an internal url format
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getUrlValidationRules(): array
|
|
{
|
|
return ['nullable', function ($attribute, $value, $fail) {
|
|
$params = collect(\explode('#', \ltrim(\rtrim($value, '#'), '#')));
|
|
if (
|
|
!\in_array(
|
|
$params->get(0),
|
|
[Breeder::class, Recipe::class]
|
|
)
|
|
) {
|
|
$fail(trans('Invalid internal url'));
|
|
return;
|
|
}
|
|
// Check if model id exists. Param 0 is class, 1 is id.
|
|
if (!\forward_static_call([$params->get(0), 'find'], $params->get(1))) {
|
|
$fail(trans("$attribute n\'est plus un lien valide."));
|
|
}
|
|
}];
|
|
}
|
|
}
|