ldc-cocotte/app/Http/Requests/Bo/Slides/StoreSlideRequest.php
Jérémy Munsch d0efb0f527
refactor: update namespaces for actions and queries
Refactored the codebase to change the namespaces of various actions and queries from `App\Actions` to `App\Cqs\Actions` and `App\Queries` to `App\Cqs\Queries`. This restructuring improves organization and clarity in the codebase.
2026-11-10 15:13:26 +01:00

155 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Bo\Slides;
use App\Cqs\Actions\Slides\CreateSlideInput;
use App\Traits\Requests\HasPicture;
use Domain\Models\ValueObjects\DatePeriod;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Date;
use Infrastructure\Adapters\Services\AppAdminAuthService;
class StoreSlideRequest extends FormRequest
{
use HasPicture;
public function __construct(
private readonly AppAdminAuthService $authService
) {
}
public function authorize(): bool
{
return $this->authService->check();
}
public function resolvePicture(): UploadedFile | string | null
{
$file = $this->file('picture');
if ($file instanceof UploadedFile) {
return $file;
}
return $this->has('picture') && is_string($this->input('picture'))
? $this->input('picture')
: null;
}
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->resolvePicture(),
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->input('text');
if (is_string($text) && trim(html_entity_decode(strip_tags($text), ENT_QUOTES | ENT_HTML5, 'UTF-8')) === '') {
$text = null;
}
$this->merge([
'text' => $text,
'connected' => $this->boolean('connected'),
'link_new_tab' => $this->boolean('link_new_tab'),
]);
}
/** @return array<string,mixed> */
public function rules(): array
{
$maxWidth = 1440;
$maxHeight = 960;
return array_merge(
[
'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'],
'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'],
],
$this->pictureRules(
'picture',
$maxWidth,
$maxHeight,
$maxWidth,
$maxHeight,
true
)
);
}
#[\Override]
protected function passedValidation()
{
if ($this->filled('date_start')) {
$inputValue = Date::createFromFormat(
'Y-m-d\TH:i',
$this->string('date_start')->value(),
$this->string('client_timezone')->value()
)?->setTimezone('UTC');
$this->merge([
'date_start' => $inputValue?->format('Y-m-d H:i:s'),
]);
}
if ($this->filled('date_end')) {
$inputValue = Date::createFromFormat(
'Y-m-d\TH:i',
$this->string('date_end')->value(),
$this->string('client_timezone')->value()
)?->setTimezone('UTC');
$this->merge([
'date_end' => $inputValue?->format('Y-m-d H:i:s'),
]);
}
}
#[\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'),
];
}
}