ldc-crm/app/Http/Requests/Api/V2/StoreCustomerRequest.php
Alexis Gousseau 303834bbca
Crm V4
- Make all tests pass Client (V4),CRMV2,CRMV4
- Simplify client submodule path
- Move App_url to .env.testing
2023-10-06 16:37:29 +02:00

98 lines
2.9 KiB
PHP

<?php
namespace App\Http\Requests\Api\V2;
use App\Enums\Country;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\Rules\Password;
/**
* @codeCoverageIgnore
*/
class StoreCustomerRequest extends BaseApiRequest
{
/**
* Current country
*
* @var \App\Enums\Country|null
*/
protected $currentCountry = null;
/**
* 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();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
$countryList = ($this->currentCountry && $this->currentCountry->get('iso')) ?
$this->currentCountry->get('iso') : implode(',', Country::getValidIsoAplha2Codes());
return [
'firstname' => 'required|string|min:1|max:255',
'lastname' => 'required|string|min:1|max:255',
'email' => 'required|email:rfc,dns,spoof,filter|max:255',
'password' => [
'required',
'max:255',
Password::min(8)
->mixedCase()
->letters()
->numbers()
->symbols()
->uncompromised()
],
'birth_date' => 'required|date_format:Y-m-d|max:255',
'address' => 'sometimes|required_with:address2|string|max:255',
'address2' => 'sometimes|string|max:255',
'phone' => ['sometimes', 'string', "phone:{$countryList}"],
'country' => [
'sometimes',
'numeric',
new Enum(Country::class),
sprintf(
'in:%s',
implode(',', Country::getValidCountriesValues())
)
],
'city' => 'sometimes|string|min:2|max:255',
'postcode' => ['sometimes', 'string', "postal_code:{$countryList}"],
'opt_in' => 'required|boolean',
];
}
/**
* Messages.
*
* @return array
*/
public function messages(): array
{
return [
'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())]
)
];
}
}