2021-03-14 10:45:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
2021-07-28 14:18:47 +02:00
|
|
|
namespace App\Http\Requests\Api\V2;
|
2021-03-14 10:45:42 +01:00
|
|
|
|
|
|
|
|
use App\Enums\Country;
|
2023-09-04 13:08:34 +02:00
|
|
|
use Illuminate\Validation\Rules\Enum;
|
2021-08-06 15:57:48 +02:00
|
|
|
use Illuminate\Validation\Rules\Password;
|
2021-03-14 10:45:42 +01:00
|
|
|
|
2023-09-06 17:24:53 +02:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
2021-03-14 10:45:42 +01:00
|
|
|
class StoreCustomerRequest extends BaseApiRequest
|
|
|
|
|
{
|
2022-06-09 11:09:11 +02:00
|
|
|
/**
|
|
|
|
|
* Current country
|
|
|
|
|
*
|
|
|
|
|
* @var \App\Enums\Country|null
|
|
|
|
|
*/
|
|
|
|
|
protected $currentCountry = null;
|
|
|
|
|
|
2023-09-06 17:24:53 +02:00
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function prepareForValidation(): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->currentCountry = Country::make(\intval($this->country));
|
|
|
|
|
} catch (\TypeError | \BadMethodCallException $e) {
|
|
|
|
|
$this->currentCountry = null;
|
|
|
|
|
}
|
|
|
|
|
parent::prepareForValidation();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
2023-09-06 17:24:53 +02:00
|
|
|
$countryList = ($this->currentCountry && $this->currentCountry->get('iso')) ?
|
|
|
|
|
$this->currentCountry->get('iso') : implode(',', Country::getValidIsoAplha2Codes());
|
2021-03-14 10:45:42 +01:00
|
|
|
return [
|
2023-09-04 13:08:34 +02:00
|
|
|
'firstname' => 'required|string|min:1|max:255',
|
|
|
|
|
'lastname' => 'required|string|min:1|max:255',
|
|
|
|
|
'email' => 'required|email:rfc,dns,spoof,filter|max:255',
|
|
|
|
|
'password' => [
|
2021-03-14 10:45:42 +01:00
|
|
|
'required',
|
2021-10-20 15:30:21 +02:00
|
|
|
'max:255',
|
2021-08-09 11:40:46 +02:00
|
|
|
Password::min(8)
|
2021-08-06 15:57:48 +02:00
|
|
|
->mixedCase()
|
|
|
|
|
->letters()
|
|
|
|
|
->numbers()
|
|
|
|
|
->symbols()
|
|
|
|
|
->uncompromised()
|
2021-03-14 10:45:42 +01:00
|
|
|
],
|
2021-10-20 15:30:21 +02:00
|
|
|
'birth_date' => 'required|date_format:Y-m-d|max:255',
|
2023-09-04 13:08:34 +02:00
|
|
|
'address' => 'sometimes|required_with:address2|string|max:255',
|
|
|
|
|
'address2' => 'sometimes|string|max:255',
|
2023-09-06 17:24:53 +02:00
|
|
|
'phone' => ['sometimes', 'string', "phone:{$countryList}"],
|
2023-09-04 13:08:34 +02:00
|
|
|
'country' => [
|
2021-03-14 10:45:42 +01:00
|
|
|
'sometimes',
|
2021-10-20 15:30:21 +02:00
|
|
|
'numeric',
|
2023-09-04 13:08:34 +02:00
|
|
|
new Enum(Country::class),
|
2021-10-20 15:30:21 +02:00
|
|
|
sprintf(
|
|
|
|
|
'in:%s',
|
2023-09-04 13:08:34 +02:00
|
|
|
implode(',', Country::getValidCountriesValues())
|
2021-10-20 15:30:21 +02:00
|
|
|
)
|
2021-03-14 10:45:42 +01:00
|
|
|
],
|
2023-09-04 13:08:34 +02:00
|
|
|
'city' => 'sometimes|string|min:2|max:255',
|
2023-09-06 17:24:53 +02:00
|
|
|
'postcode' => ['sometimes', 'string', "postal_code:{$countryList}"],
|
2023-09-04 13:08:34 +02:00
|
|
|
'opt_in' => 'required|boolean',
|
2021-03-14 10:45:42 +01:00
|
|
|
];
|
|
|
|
|
}
|
2021-10-20 15:30:21 +02:00
|
|
|
|
2022-06-09 11:09:11 +02:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Messages.
|
2021-12-19 15:57:24 +01:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function messages(): array
|
2021-10-20 15:30:21 +02:00
|
|
|
{
|
|
|
|
|
return [
|
2023-09-04 13:08:34 +02:00
|
|
|
'phone.phone' => trans(
|
|
|
|
|
'Le champ :attribute est invalide, il accepte les numéros des pays
|
|
|
|
|
suivants :countries.',
|
|
|
|
|
['countries' => implode(',', Country::getValidCountriesLabel())]
|
|
|
|
|
),
|
|
|
|
|
'country.in' => trans(
|
|
|
|
|
'Le champ :attribute est invalide, il accepte les pays suivants
|
|
|
|
|
:countries.',
|
|
|
|
|
['countries' => implode(',', Country::getValidCountriesLabel())]
|
|
|
|
|
)
|
2021-10-20 15:30:21 +02:00
|
|
|
];
|
|
|
|
|
}
|
2021-03-14 10:45:42 +01:00
|
|
|
}
|