63 lines
1.8 KiB
PHP

<?php declare(strict_types=1);
namespace App\Http\Requests\Site\Feedback;
use App\Contracts\FormRequestDto;
use App\Dto\HttpUserData;
use App\Dto\Service\Site\Feedback\Send;
use App\Services\WebsiteTranslations;
use Illuminate\Foundation\Http\FormRequest;
final class SendRequest extends FormRequest implements FormRequestDto
{
public function attributes(): array
{
$attributes = [];
$websiteTranslations = $this->request->get('websiteTranslations', null);
if ($websiteTranslations) {
/** @var WebsiteTranslations $websiteTranslations */
$attributes = [
'name' => $websiteTranslations->translate('site.attributes.name'),
'email' => $websiteTranslations->translate('site.attributes.email'),
'message' => $websiteTranslations->translate('site.attributes.message'),
];
}
return $attributes;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$rules = [
'name' => ['nullable', 'string', 'max:255'],
'email' => ['nullable', 'string', 'max:255', 'email'],
'message' => ['required', 'string', 'max:5000'],
];
if (config('app.captcha', false)) {
$rules['captcha-verified'] = ['captcha'];
}
return $rules;
}
public function getDto(): Send
{
$httpUserData = new HttpUserData(
clientIp: $this->getClientIp(),
userAgent: $this->userAgent(),
);
return new Send(
message: $this->input('message'),
httpUserData: $httpUserData,
name: $this->input('name', null),
email: $this->input('email', null),
);
}
}