102 lines
3.4 KiB
PHP
102 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Fo;
|
|
|
|
use App\Cqs\Actions\Contact\SubmitContactFormInput;
|
|
use App\Rules\FileExtensionMatchesMimeType;
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class ContactFormRequest extends FormRequest
|
|
{
|
|
public const int MAX_FILE_SIZE_KB = 5120;
|
|
|
|
/** @var list<string> */
|
|
public const array ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'pdf'];
|
|
|
|
/** @var list<string> */
|
|
public const array ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'application/pdf'];
|
|
|
|
/** @return array<string,mixed> */
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'last_name' => ['required', 'string', 'min:2'],
|
|
'first_name' => ['required', 'string', 'min:2'],
|
|
'email' => [
|
|
'required',
|
|
Rule::email()
|
|
->rfcCompliant()
|
|
->preventSpoofing()
|
|
->validateMxRecord(),
|
|
],
|
|
'subject' => ['required', 'string', 'min:2'],
|
|
'order_ref' => [
|
|
'nullable',
|
|
'string',
|
|
static function (string $attribute, mixed $value, \Closure $fail): void {
|
|
if (! is_string($value) || $value === '') {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
OrderReference::fromString($value);
|
|
} catch (\DomainException) {
|
|
$fail(trans('validation.regex', ['attribute' => $attribute]));
|
|
}
|
|
},
|
|
],
|
|
'message' => ['required', 'string', 'min:20', 'max:1900'],
|
|
'file' => [
|
|
'nullable',
|
|
Rule::file()
|
|
->types([...self::ALLOWED_EXTENSIONS, ...self::ALLOWED_MIME_TYPES])
|
|
->extensions(self::ALLOWED_EXTENSIONS)
|
|
->rules([new FileExtensionMatchesMimeType()])
|
|
->max(self::MAX_FILE_SIZE_KB),
|
|
],
|
|
'ok_cgv' => ['accepted'],
|
|
];
|
|
}
|
|
|
|
/** @return array<string,string> */
|
|
#[\Override]
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'ok_cgv.accepted' => trans('validation.custom.ok_cgv.accepted'),
|
|
'file.max' => trans('validation.custom.file.max'),
|
|
'file.mimes' => trans('validation.custom.file.mimes'),
|
|
];
|
|
}
|
|
|
|
public function toSubmitContactFormInput(): SubmitContactFormInput
|
|
{
|
|
$file = $this->file('file');
|
|
$uploadedFile = $file instanceof UploadedFile ? $file : null;
|
|
$orderRef = $this->input('order_ref');
|
|
|
|
return new SubmitContactFormInput(
|
|
lastName: $this->string('last_name')->value(),
|
|
firstName: $this->string('first_name')->value(),
|
|
email: $this->string('email')->value(),
|
|
subject: $this->string('subject')->value(),
|
|
message: $this->string('message')->value(),
|
|
orderRef: empty($orderRef) ? null : OrderReference::fromString($this->string('order_ref')->value()),
|
|
file: $uploadedFile,
|
|
);
|
|
}
|
|
|
|
/** @return array<string,string> */
|
|
#[\Override]
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'subject' => trans('validation.custom.attributes.object'),
|
|
];
|
|
}
|
|
}
|