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;
|
2021-08-06 15:57:48 +02:00
|
|
|
use Illuminate\Validation\Rules\Password;
|
2021-10-20 15:30:21 +02:00
|
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
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;
|
|
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2021-10-20 15:30:21 +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',
|
2021-03-14 10:45:42 +01:00
|
|
|
'password' => [
|
|
|
|
|
'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',
|
|
|
|
|
'address' => 'sometimes|required_with:address2|string|max:255',
|
|
|
|
|
'address2' => 'sometimes|string|max:255',
|
2022-06-09 11:09:11 +02:00
|
|
|
'phone' => ['sometimes', 'string', 'phone:' . ($this->currentCountry ?
|
|
|
|
|
$this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes()))],
|
2021-03-14 10:45:42 +01:00
|
|
|
'country' => [
|
|
|
|
|
'sometimes',
|
2021-10-20 15:30:21 +02:00
|
|
|
'numeric',
|
|
|
|
|
new EnumIsValidRule(Country::class),
|
|
|
|
|
sprintf(
|
|
|
|
|
'in:%s',
|
2022-06-09 11:09:11 +02:00
|
|
|
implode(',', Country::getValidCountries())
|
2021-10-20 15:30:21 +02:00
|
|
|
)
|
2021-03-14 10:45:42 +01:00
|
|
|
],
|
2021-10-20 15:30:21 +02:00
|
|
|
'city' => 'sometimes|string|min:2|max:255',
|
2022-06-09 11:09:11 +02:00
|
|
|
'postcode' => ['sometimes', 'string', 'postal_code:' . ($this->currentCountry ?
|
|
|
|
|
$this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes()))],
|
2021-08-09 11:40:46 +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
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function prepareForValidation(): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
if (is_array($this->country)) {
|
|
|
|
|
$this->currentCountry = Country::make(\intval($this->country));
|
|
|
|
|
}
|
|
|
|
|
} catch (\TypeError | \BadMethodCallException $e) {
|
|
|
|
|
$this->currentCountry = null;
|
|
|
|
|
}
|
|
|
|
|
parent::prepareForValidation();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Messages
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-10-20 15:30:21 +02:00
|
|
|
public function messages()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
2022-06-09 11:09:11 +02:00
|
|
|
'phone.phone' => trans('Le champ :attribute est invalide, il accepte les numéros des pays suivants :countries.', [
|
|
|
|
|
'countries' => implode(',', Country::getValidCountriesLabel())
|
|
|
|
|
]),
|
2021-10-20 15:30:21 +02:00
|
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
2022-06-09 11:09:11 +02:00
|
|
|
'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
|
|
|
}
|