ldc-crm/app/Http/Requests/Api/V2/UpdateCustomerRequest.php

89 lines
3.4 KiB
PHP
Raw Normal View History

2021-03-14 10:45:42 +01:00
<?php
namespace App\Http\Requests\Api\V2;
2021-03-14 10:45:42 +01:00
use App\Enums\Country;
use Illuminate\Validation\Rules\Password;
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
2021-03-14 10:45:42 +01:00
class UpdateCustomerRequest extends BaseApiRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
$fields = [
'firstname', 'lastname', 'email', 'password',
'birth_date', 'address', 'phone', 'country',
'city', 'postcode', 'opt_in'
];
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
$atLeast = function ($attribute, $value, $fail) use ($fields) {
if (!\in_array($attribute, $fields)) {
$fail(trans('require_at_least_field', [
'attributes' => rtrim(\implode(',', $fields), ',')
]));
}
};
return [
'firstname' => [$atLeast, 'string', 'min:1', 'max:255'],
'lastname' => [$atLeast, 'string', 'min:1', 'max:255'],
'email' => [$atLeast, 'email:rfc,dns,spoof,filter', 'max:255'],
'password' => [
$atLeast, 'max:255', Password::min(8)
->mixedCase()
->letters()
->numbers()
->symbols()
->uncompromised()
],
'birth_date' => [$atLeast, 'date_format:Y-m-d', 'max:255'],
'address' => [$atLeast, 'required_with:address2', 'string', 'max:255'],
'address2' => ['sometimes', 'string', 'max:255'],
'phone' => [$atLeast, 'string', 'phone:FR,BE,LU', 'max:255'],
2021-03-14 10:45:42 +01:00
'country' => [
$atLeast,
'numeric',
new EnumIsValidRule(Country::class),
sprintf(
'in:%s',
implode(',', [
Country::belgium()->value,
Country::luxembourg()->value,
Country::france()->value,
// DROM COM
Country::guadeloupe()->value,
Country::martinique()->value,
Country::frenchGuiana()->value,
Country::reunion()->value,
Country::saintPierreAndMiquelon()->value,
Country::mayotte()->value,
Country::saintBarthelemy()->value,
Country::saintMartinFrenchPart()->value,
Country::wallisAndFutuna()->value,
Country::frenchPolynesia()->value,
Country::newCaledonia()->value
])
)
2021-03-14 10:45:42 +01:00
],
'city' => [$atLeast, 'string', 'min:2', 'max:255'],
'postcode' => [$atLeast, 'string', 'postal_code:FR,BE,LU', 'max:255'],
2021-03-14 10:45:42 +01:00
'opt_in' => [$atLeast, 'boolean'],
];
}
public function messages()
{
return [
// phpcs:ignore Generic.Files.LineLength.TooLong
'phone.phone' => trans('Le champ :attribute est invalide, il accepte les numéros des pays suivants France, Belgique, Luxembourg.'),
// phpcs:ignore Generic.Files.LineLength.TooLong
'country.in' => trans('Le champ :attribute est invalide, il accepte les pays suivants France, Belgique, Luxembourg.')
];
}
2021-03-14 10:45:42 +01:00
}