2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Bo\Ranges;
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Actions\Ranges\CreateRangeInput;
|
2026-11-14 16:06:34 +01:00
|
|
|
use App\Rules\FileExtensionMatchesMimeType;
|
2026-10-01 11:25:05 +02:00
|
|
|
use Domain\Models\ValueObjects\PictureMetadata;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2026-11-14 16:06:34 +01:00
|
|
|
use Illuminate\Validation\Rule;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
|
|
|
|
|
|
|
|
|
class StoreRangeRequest extends FormRequest
|
|
|
|
|
{
|
2026-11-14 16:06:34 +01:00
|
|
|
public const int MAX_FILE_SIZE_KB = 5120;
|
|
|
|
|
|
|
|
|
|
public const int PICTURE_WIDTH = 500;
|
|
|
|
|
|
|
|
|
|
public const int PICTURE_HEIGHT = 500;
|
|
|
|
|
|
|
|
|
|
/** @var list<string> */
|
|
|
|
|
public const array ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png'];
|
|
|
|
|
|
|
|
|
|
/** @var list<string> */
|
|
|
|
|
public const array ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png'];
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
public function __construct(
|
2026-11-14 16:06:34 +01:00
|
|
|
private readonly AppAdminAuthService $authService,
|
2026-06-14 21:32:07 +02:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function authorize(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->authService->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
protected function prepareForValidation(): void
|
|
|
|
|
{
|
|
|
|
|
$this->merge([
|
|
|
|
|
'status' => $this->boolean('status'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 16:06:34 +01:00
|
|
|
/** @return array<string, \Illuminate\Validation\Rule|array<mixed>|string> */
|
2026-06-14 21:32:07 +02:00
|
|
|
public function rules(): array
|
|
|
|
|
{
|
2026-11-14 16:06:34 +01:00
|
|
|
return [
|
|
|
|
|
'name' => ['required', 'string', 'min:2', 'max:45'],
|
|
|
|
|
'description' => ['nullable', 'string', 'min:2'],
|
|
|
|
|
'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)
|
|
|
|
|
),
|
2026-06-14 21:32:07 +02:00
|
|
|
],
|
2026-11-14 16:06:34 +01:00
|
|
|
'status' => ['present', 'boolean:strict'],
|
|
|
|
|
];
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function attributes(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'name' => trans('validation.attributes.name'),
|
|
|
|
|
'description' => trans('validation.attributes.description'),
|
|
|
|
|
'status' => trans('validation.custom.attributes.status'),
|
|
|
|
|
'picture' => trans('validation.attributes.image'),
|
|
|
|
|
'picture_alt' => trans('validation.custom.attributes.picture_alt'),
|
|
|
|
|
'picture_title' => trans('validation.custom.attributes.picture_title'),
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-10-01 11:25:05 +02:00
|
|
|
|
|
|
|
|
public function toCreateRangeInput(): CreateRangeInput
|
|
|
|
|
{
|
|
|
|
|
$pictureAlt = $this->filled('picture_alt') ? $this->string('picture_alt')->value() : null;
|
|
|
|
|
$pictureTitle = $this->filled('picture_title') ? $this->string('picture_title')->value() : null;
|
|
|
|
|
|
|
|
|
|
return new CreateRangeInput(
|
|
|
|
|
name: $this->string('name')->value(),
|
|
|
|
|
description: $this->filled('description') ? $this->string('description')->value() : null,
|
2026-11-14 16:06:34 +01:00
|
|
|
picture: $this->hasFile('picture') ? $this->file('picture') : null,
|
2026-10-01 11:25:05 +02:00
|
|
|
pictureMetadata: new PictureMetadata(
|
|
|
|
|
alt: $pictureAlt,
|
|
|
|
|
title: $pictureTitle,
|
|
|
|
|
),
|
|
|
|
|
status: $this->boolean('status'),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|