91 lines
2.9 KiB
PHP
91 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Fo;
|
|
|
|
use Domain\Models\ValueObjects\AttachedFile;
|
|
use Domain\Models\ValueObjects\ContactFormData;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class ContactFormRequest extends FormRequest
|
|
{
|
|
/** @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', 'regex:/^[0-9]+$/'],
|
|
'message' => ['required', 'string', 'min:20', 'max:1900'],
|
|
'file' => [
|
|
'nullable',
|
|
Rule::file()
|
|
->max(5120)
|
|
->types(['jpg', 'jpeg', 'png', 'pdf']),
|
|
],
|
|
'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 toValueObject(): ContactFormData
|
|
{
|
|
$attachedFile = null;
|
|
|
|
if ($this->hasFile('file') && $this->file('file')->isValid()) {
|
|
$uploadedFile = $this->file('file');
|
|
$storedPath = $uploadedFile->store('contact-attachments', 'private');
|
|
|
|
if ($storedPath !== false) {
|
|
$fullPath = Storage::disk('private')->path($storedPath);
|
|
|
|
$attachedFile = new AttachedFile(
|
|
path: $fullPath,
|
|
originalName: $uploadedFile->getClientOriginalName(),
|
|
mimeType: $uploadedFile->getMimeType() ?? 'application/octet-stream',
|
|
);
|
|
}
|
|
}
|
|
|
|
return new ContactFormData(
|
|
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($this->input('order_ref')) ?
|
|
null : $this->string('order_ref')->value(),
|
|
file: $attachedFile,
|
|
);
|
|
}
|
|
|
|
/** @return array<string,string> */
|
|
#[\Override]
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'subject' => trans('validation.custom.attributes.object'),
|
|
];
|
|
}
|
|
}
|