ldc-cocotte/app/Http/Requests/Bo/Slides/StoreSlideRequest.php

141 lines
5.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Bo\Slides;
use App\Cqs\Actions\Slides\CreateSlideInput;
use App\Rules\FileExtensionMatchesMimeType;
use App\Traits\Requests\ConvertsClientDatesToUtc;
use Domain\Models\ValueObjects\DatePeriod;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Stringable;
use Illuminate\Validation\Rule;
use Infrastructure\Adapters\Services\AppAdminAuthService;
class StoreSlideRequest extends FormRequest
{
use ConvertsClientDatesToUtc;
public const int MAX_FILE_SIZE_KB = 5120;
public const int PICTURE_WIDTH = 1440;
public const int PICTURE_HEIGHT = 960;
/** @var list<string> */
public const array ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png'];
/** @var list<string> */
public const array ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png'];
public function __construct(
private readonly AppAdminAuthService $authService,
) {
}
public function authorize(): bool
{
return $this->authService->check();
}
public function toDatePeriod(): DatePeriod
{
return new DatePeriod(
start: $this->filled('date_start') ? new \DateTimeImmutable($this->string('date_start')->value()) : null,
end: $this->filled('date_end') ? new \DateTimeImmutable($this->string('date_end')->value()) : null,
);
}
public function toCreateSlideInput(): CreateSlideInput
{
return new CreateSlideInput(
title: $this->string('title')->value(),
text: $this->filled('text') ? $this->string('text')->value() : null,
picture: $this->hasFile('picture') ? $this->file('picture') : null,
pictureAlt: $this->filled('picture_alt') ? $this->string('picture_alt')->value() : null,
pictureTitle: $this->filled('picture_title') ? $this->string('picture_title')->value() : null,
link: $this->filled('link') ? $this->string('link')->value() : null,
linkLabel: $this->filled('link_label') ? $this->string('link_label')->value() : null,
linkNewTab: $this->boolean('link_new_tab'),
connected: $this->boolean('connected'),
period: $this->toDatePeriod(),
);
}
#[\Override]
protected function prepareForValidation(): void
{
$text = $this->string('text')->value();
$trimmedText = $this->string('text')
->pipe(fn (Stringable $text) => html_entity_decode(strip_tags((string) $text), ENT_QUOTES | ENT_HTML5, 'UTF-8'))
->trim()
->toString();
if ($trimmedText === '') {
$text = null;
}
$this->merge([
'text' => $text,
'connected' => $this->boolean('connected'),
'link_new_tab' => $this->boolean('link_new_tab'),
]);
}
/** @return array<string, \Illuminate\Validation\Rule|array<mixed>|string> */
public function rules(): array
{
return [
'title' => ['required', 'string', 'min:4', 'max:75'],
'picture_alt' => ['required_with:picture', 'string', 'min:4', 'max:75'],
'picture_title' => ['required_with:picture', 'string', 'min:4', 'max:75'],
'picture' => [
Rule::imageFile()
->extensions(self::ALLOWED_EXTENSIONS)
->rules([
'mimetypes:' . implode(',', self::ALLOWED_MIME_TYPES),
new FileExtensionMatchesMimeType(),
])
->max(self::MAX_FILE_SIZE_KB)
->dimensions(
Rule::dimensions()
->width(static::PICTURE_WIDTH)
->height(static::PICTURE_HEIGHT)
),
],
'text' => ['nullable', 'required_without:picture', 'string', 'min:4'],
'link' => ['nullable', 'url:https', 'max:255'],
'link_label' => ['nullable', 'string', 'min:5', 'max:255'],
'link_new_tab' => ['present', 'boolean:strict'],
'connected' => ['present', 'boolean:strict'],
'date_start' => ['nullable', 'date_format:Y-m-d\TH:i'],
'date_end' => ['nullable', 'date_format:Y-m-d\TH:i', 'after:date_start'],
'client_timezone' => ['required', 'timezone'],
];
}
#[\Override]
protected function passedValidation(): void
{
$this->mergeLocalDatetimeAsUtc('date_start');
$this->mergeLocalDatetimeAsUtc('date_end');
}
#[\Override]
public function attributes(): array
{
return [
'title' => trans('validation.attributes.title'),
'text' => trans('validation.attributes.text'),
'date_start' => trans('validation.custom.attributes.date_start_show'),
'date_end' => trans('validation.custom.attributes.date_end_show'),
'connected' => trans('validation.custom.attributes.connected'),
'link' => trans('validation.custom.attributes.link'),
'link_label' => trans('validation.custom.attributes.link_label'),
'link_new_tab' => trans('validation.custom.attributes.link_new_tab'),
'picture' => trans('validation.attributes.image'),
'picture_alt' => trans('validation.custom.attributes.picture_alt'),
'picture_title' => trans('validation.custom.attributes.picture_title'),
];
}
}