Updated the address attribute in multiple requests and views to use the new 'postal_address' translation key for consistency.
119 lines
4.3 KiB
PHP
119 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Fo\Auth;
|
|
|
|
use App\Actions\Customers\RegisterCustomerInput;
|
|
use App\Rules\Adult;
|
|
use Domain\Models\ValueObjects\Code;
|
|
use Domain\Models\ValueObjects\Email;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Domain\Models\ValueObjects\PhoneNumber;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Rules\Password;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
class RegisterRequest extends FormRequest
|
|
{
|
|
public function __construct(
|
|
private readonly AppCustomerAuthService $authService
|
|
) {
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return $this->authService->guest();
|
|
}
|
|
|
|
#[\Override]
|
|
public function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'optin' => $this->boolean('optin'),
|
|
]);
|
|
}
|
|
|
|
public function toRegisterCustomerInput(): RegisterCustomerInput
|
|
{
|
|
$birthDate = \DateTimeImmutable::createFromFormat(
|
|
'Y-m-d',
|
|
$this->string('birth_date')->value()
|
|
) ?: throw new \InvalidArgumentException('Invalid birth date format');
|
|
|
|
$phone = $this->filled('phone')
|
|
? new PhoneNumber($this->string('phone')->value())
|
|
: null;
|
|
|
|
return new RegisterCustomerInput(
|
|
matricule: new Matricule($this->string('matricule')->value()),
|
|
code: new Code($this->string('code')->value()),
|
|
firstname: $this->string('firstname')->value(),
|
|
lastname: $this->string('lastname')->value(),
|
|
email: new Email($this->string('email')->value()),
|
|
password: $this->string('password')->value(),
|
|
birthDate: $birthDate,
|
|
phone: $phone,
|
|
address: $this->filled('address') ? $this->string('address')->value() : null,
|
|
postcode: $this->string('postal_code')->value(),
|
|
city: $this->string('city')->value(),
|
|
optIn: $this->boolean('optin'),
|
|
);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'matricule' => ['required', 'exists:employees,matricule'],
|
|
'code' => [
|
|
'required',
|
|
Rule::exists('employees', 'code')
|
|
->where('matricule', $this->string('matricule')->value()),
|
|
],
|
|
'lastname' => ['required', 'string', 'max:255'],
|
|
'firstname' => ['required', 'string', 'max:255'],
|
|
'phone' => ['sometimes', 'phone:FR', 'max:255'],
|
|
'address' => ['sometimes', 'max:255'],
|
|
'postal_code' => ['required', 'postal_code:FR'],
|
|
'city' => ['required', 'string', 'max:255'],
|
|
'email' => ['required', 'unique:customers,email', 'email:rfc,dns,spoof', 'max:255'],
|
|
'password' => [
|
|
'required',
|
|
'required_with:password_confirmation',
|
|
'confirmed',
|
|
'max:255',
|
|
Password::min(8)
|
|
->mixedCase()
|
|
->letters()
|
|
->numbers()
|
|
->symbols()
|
|
->uncompromised(),
|
|
],
|
|
'ok_cgv' => ['accepted'],
|
|
'optin' => ['sometimes', 'boolean'],
|
|
'birth_date' => ['required', new Adult()],
|
|
];
|
|
}
|
|
|
|
/** @return array<string,string> */
|
|
#[\Override]
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'firstname' => trans('validation.attributes.first_name'),
|
|
'lastname' => trans('validation.attributes.last_name'),
|
|
'birth_date' => trans('validation.attributes.date_of_birth'),
|
|
'address' => trans('validation.custom.attributes.postal_address'),
|
|
'phone' => trans('validation.attributes.phone'),
|
|
'city' => trans('validation.attributes.city'),
|
|
'postcode' => trans('validation.attributes.postal_code'),
|
|
'password' => trans('validation.attributes.password'),
|
|
'matricule' => trans('validation.attributes.matricule'),
|
|
'code' => trans('validation.attributes.code'),
|
|
'ok_cgv' => trans('validation.custom.attributes.ok_cgv'),
|
|
'optin' => trans('validation.custom.attributes.optin'),
|
|
];
|
|
}
|
|
}
|