Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
44 / 44 |
| UpdateCustomerRequest | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
44 / 44 |
| rules | |
100.00% |
1 / 1 |
1 | |
100.00% |
42 / 42 |
|||
| messages | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Requests\Api\V2; |
| 4 | |
| 5 | use App\Enums\Country; |
| 6 | use Illuminate\Validation\Rules\Password; |
| 7 | use Kwaadpepper\Enum\Rules\EnumIsValidRule; |
| 8 | |
| 9 | class UpdateCustomerRequest extends BaseApiRequest |
| 10 | { |
| 11 | /** |
| 12 | * Get the validation rules that apply to the request. |
| 13 | * |
| 14 | * @return array |
| 15 | */ |
| 16 | public function rules(): array |
| 17 | { |
| 18 | $fields = [ |
| 19 | 'firstname', 'lastname', 'email', 'password', |
| 20 | 'birth_date', 'address', 'phone', 'country', |
| 21 | 'city', 'postcode', 'opt_in' |
| 22 | ]; |
| 23 | $atLeast = function ($attribute) use ($fields) { |
| 24 | return 'required_without_all:' . collect($fields) |
| 25 | ->filter(function ($fieldName) use ($attribute) { |
| 26 | return $fieldName !== $attribute; |
| 27 | })->implode(','); |
| 28 | }; |
| 29 | return [ |
| 30 | 'firstname' => [$atLeast('firstname'), 'string', 'min:1', 'max:255'], |
| 31 | 'lastname' => [$atLeast('lastname'), 'string', 'min:1', 'max:255'], |
| 32 | 'email' => [$atLeast('email'), 'email:rfc,dns,spoof,filter', 'max:255'], |
| 33 | 'password' => [ |
| 34 | $atLeast('password'), 'max:255', Password::min(8) |
| 35 | ->mixedCase() |
| 36 | ->letters() |
| 37 | ->numbers() |
| 38 | ->symbols() |
| 39 | ->uncompromised() |
| 40 | ], |
| 41 | 'birth_date' => [$atLeast('birth_date'), 'date_format:Y-m-d', 'max:255'], |
| 42 | 'address' => [$atLeast('address'), 'required_with:address2', 'string', 'max:255'], |
| 43 | 'address2' => ['sometimes', 'string', 'max:255'], |
| 44 | 'phone' => [$atLeast('phone'), 'string', 'phone:FR,BE,LU', 'max:255'], |
| 45 | 'country' => [ |
| 46 | $atLeast('country'), |
| 47 | 'numeric', |
| 48 | new EnumIsValidRule(Country::class), |
| 49 | sprintf( |
| 50 | 'in:%s', |
| 51 | implode(',', [ |
| 52 | Country::belgium()->value, |
| 53 | Country::luxembourg()->value, |
| 54 | Country::france()->value, |
| 55 | // * DROM COM. |
| 56 | Country::guadeloupe()->value, |
| 57 | Country::martinique()->value, |
| 58 | Country::frenchGuiana()->value, |
| 59 | Country::reunion()->value, |
| 60 | Country::saintPierreAndMiquelon()->value, |
| 61 | Country::mayotte()->value, |
| 62 | Country::saintBarthelemy()->value, |
| 63 | Country::saintMartinFrenchPart()->value, |
| 64 | Country::wallisAndFutuna()->value, |
| 65 | Country::frenchPolynesia()->value, |
| 66 | Country::newCaledonia()->value |
| 67 | ]) |
| 68 | ) |
| 69 | ], |
| 70 | 'city' => [$atLeast('city'), 'string', 'min:2', 'max:255'], |
| 71 | 'postcode' => [$atLeast('postcode'), 'string', 'postal_code:FR,BE,LU', 'max:255'], |
| 72 | 'opt_in' => [$atLeast('opt_in'), 'boolean'], |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Messages |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public function messages() |
| 82 | { |
| 83 | return [ |
| 84 | // phpcs:ignore Generic.Files.LineLength.TooLong |
| 85 | 'phone.phone' => trans('Le champ :attribute est invalide, il accepte les numéros des pays suivants France, Belgique, Luxembourg.'), |
| 86 | // phpcs:ignore Generic.Files.LineLength.TooLong |
| 87 | 'country.in' => trans('Le champ :attribute est invalide, il accepte les pays suivants France, Belgique, Luxembourg.') |
| 88 | ]; |
| 89 | } |
| 90 | } |