111 lines
4.3 KiB
PHP
111 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Back\News;
|
|
|
|
use App\Enums\News\NewsStatusEnum;
|
|
use App\Models\News;
|
|
use App\Rules\Video;
|
|
use App\Traits\Requests\HasInertiaPicture;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Validation\Rules\Enum;
|
|
use Uwil\LaravelCommon\Rules\IsValidVimeoId;
|
|
use Uwil\LaravelCommon\Rules\WysiwygLength;
|
|
|
|
class StoreNewsRequest extends FormRequest
|
|
{
|
|
use HasInertiaPicture;
|
|
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Gate::check('create', News::class);
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->mergeInertiaPicture();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'seo_title' => 'required|string|between:3,255',
|
|
'seo_description' => 'required|string|between:3,255',
|
|
'title' => 'required|string|between:3,255',
|
|
'slug' => 'required|string|between:3,255',
|
|
'picture' => [
|
|
function ($attribute, $value, $fail) {
|
|
$extension = null;
|
|
if ($value instanceof \Illuminate\Http\UploadedFile) {
|
|
$extension = strtolower($value->getClientOriginalExtension());
|
|
} elseif (is_string($value)) {
|
|
$extension = strtolower(pathinfo($value, PATHINFO_EXTENSION));
|
|
}
|
|
if ($extension === 'gif') {
|
|
$rules = 'required|file|mimes:gif|max:5120';
|
|
} else {
|
|
$rules = 'required|file|mimes:jpg,jpeg,png,webp|max:5120|dimensions:width=525,height=295';
|
|
}
|
|
/** @var callable[] $validatorClosures */
|
|
$validatorClosures = $this->pictureRules($rules);
|
|
|
|
foreach ($validatorClosures as $validatorClosure) {
|
|
$validatorClosure($attribute, $value, $fail);
|
|
}
|
|
}
|
|
],
|
|
'picture_alt' => 'nullable|string|between:3,60',
|
|
'picture_title' => 'nullable|string|between:3,60',
|
|
'content' => ['required', 'string', new WysiwygLength(10, 5000)],
|
|
'status' => ['required', new Enum(NewsStatusEnum::class)],
|
|
'categories' => 'nullable|array',
|
|
'categories.*' => 'integer|exists:news_categories,id',
|
|
'tags' => 'nullable|array',
|
|
'tags.*' => 'integer|exists:news_tags,id',
|
|
'published_at' => 'nullable|date',
|
|
'unpublished_at' => 'nullable|date|after:published_at',
|
|
'inverted' => 'boolean',
|
|
'has_show_picture' => 'boolean',
|
|
'vimeo_id' => ['string', 'nullable', 'min:8', 'max:10', new IsValidVimeoId()]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'seo_title' => trans('validation.custom.seo.title'),
|
|
'seo_description' => trans('validation.custom.seo.description'),
|
|
'title' => trans('validation.attributes.title'),
|
|
'slug' => trans('validation.custom.slug'),
|
|
'categories' => trans_choice('models.classes.news-category', \INF),
|
|
'tags' => trans_choice('models.classes.news-tag', \INF),
|
|
'picture' => trans('validation.attributes.image'),
|
|
'picture_alt' => trans('validation.custom.image_alt'),
|
|
'picture_title' => trans('validation.custom.image_title'),
|
|
'status' => trans('validation.custom.status'),
|
|
'content' => trans('validation.attributes.content'),
|
|
'published_at' => trans('validation.custom.published_at'),
|
|
'unpublished_at' => trans('validation.custom.unpublished_at'),
|
|
];
|
|
}
|
|
}
|