ldc-legaulois/app/Http/Requests/Club/Customers/StoreCustomerRequest.php

134 lines
4.7 KiB
PHP
Raw Normal View History

2024-07-30 16:05:27 +02:00
<?php
namespace App\Http\Requests\Club\Customers;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Models\Customer as CrmCustomer;
class StoreCustomerRequest extends FormRequest
{
/**
* Current country.
*
* @var \LDC\LDCCRM\Enums\Country|null
*/
protected $currentCountry = null;
2024-07-30 16:05:27 +02:00
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
2024-07-30 16:05:27 +02:00
{
return Auth::guard('club')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
public function prepareForValidation(): void
2024-07-30 16:05:27 +02:00
{
try {
$this->currentCountry = Country::make(\intval($this->country));
} catch (\TypeError | \BadMethodCallException $e) {
$this->currentCountry = null;
}
2024-07-30 16:05:27 +02:00
$this->merge([
'phone' => $this->input('phone'),
'opt_in' => $this->boolean('opt_in'),
'ok_cgu' => $this->boolean('ok_cgu'),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
2024-07-30 16:05:27 +02:00
{
$customerId = auth('club')->user();
$countryList = implode(',', Country::getValidIsoAplha2Codes());
2024-07-30 16:05:27 +02:00
return [
'lastname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'username' => ['required','string', 'max:255', Rule::unique('customers', 'username')->ignore($customerId)],
'phone' => "nullable|max:255|between:1,100|phone:{$countryList}",
'address' => 'required|string|min:1|max:255',
'address2' => 'nullable|string|min:1|max:255',
'postal_code' => "required|string|min:1,100|postal_code:{$countryList}",
'city' => 'required|string|min:2|max:255',
'birth_date' => 'required|date',
'country' => ['required', Rule::enum(Country::class), sprintf(
'in:%s',
implode(',', Country::getValidCountriesValues())
)],
'email' => [
'required',
'email:rfc,dns,spoof',
'max:255',
// Does email already exists in CRM ?
2024-07-30 16:05:27 +02:00
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function (string $attribute, $value, \Closure $fail) {
if (CrmCustomer::findWithEmail(\strval($value))) {
$fail('Un compte existe déjà avec cette adresse.');
}
},
'unique:customers,email',
],
'password' => [
'required',
'confirmed',
Password::min(8)->mixedCase()->numbers()->symbols()->uncompromised()
2024-07-30 16:05:27 +02:00
],
'ok_cgu' => 'accepted',
'opt_in' => 'required|boolean',
2024-07-30 16:05:27 +02:00
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
2024-07-30 16:05:27 +02:00
{
return [
'lastname' => trans('validation.attributes.first_name'),
'firstname' => trans('validation.attributes.first_name'),
'username' => trans('validation.attributes.username'),
'phone' => trans('validation.attributes.phone'),
'address' => trans('validation.attributes.line_address_1'),
'address2' => trans('validation.attributes.line_address_2'),
'country' => trans('validation.attributes.country'),
'postal_code' => trans('validation.attributes.postal_code'),
2024-07-30 16:05:27 +02:00
'city' => trans('validation.attributes.city'),
'birth_date' => trans('validation.attributes.date_of_birth'),
'email' => trans('validation.attributes.email'),
'password' => trans('validation.attributes.password'),
'password_confirmation' => trans('validation.attributes.password_confirmation'),
'ok_cgv' => trans('validation.custom.terms'),
'opt_in' => trans('validation.custom.newsletter'),
2024-07-30 16:05:27 +02:00
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages(): array
{
return [
'username.unique' => trans('Le pseudo est déjà pris'),
];
}
2024-07-30 16:05:27 +02:00
}