Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
23 / 25
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoreCustomerRequest
92.00% covered (success)
92.00%
23 / 25
66.67% covered (warning)
66.67%
2 / 3
7.03
0.00% covered (danger)
0.00%
0 / 1
 rules
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
 prepareForValidation
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
3.58
 messages
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Requests\Api\V2;
4
5use App\Enums\Country;
6use Illuminate\Validation\Rules\Password;
7use Kwaadpepper\Enum\Rules\EnumIsValidRule;
8
9class StoreCustomerRequest extends BaseApiRequest
10{
11    /**
12     * Current country
13     *
14     * @var \App\Enums\Country|null
15     */
16    protected $currentCountry = null;
17
18    /**
19     * Get the validation rules that apply to the request.
20     *
21     * @return array
22     */
23    public function rules(): array
24    {
25        return [
26            'firstname' => 'required|string|min:1|max:255',
27            'lastname' => 'required|string|min:1|max:255',
28            'email' => 'required|email:rfc,dns,spoof,filter|max:255',
29            'password' => [
30                'required',
31                'max:255',
32                Password::min(8)
33                    ->mixedCase()
34                    ->letters()
35                    ->numbers()
36                    ->symbols()
37                    ->uncompromised()
38            ],
39            'birth_date' => 'required|date_format:Y-m-d|max:255',
40            'address' => 'sometimes|required_with:address2|string|max:255',
41            'address2' => 'sometimes|string|max:255',
42            'phone' => ['sometimes', 'string',  'phone:' . ($this->currentCountry ?
43                $this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes()))],
44            'country' => [
45                'sometimes',
46                'numeric',
47                new EnumIsValidRule(Country::class),
48                sprintf(
49                    'in:%s',
50                    implode(',', Country::getValidCountries())
51                )
52            ],
53            'city' => 'sometimes|string|min:2|max:255',
54            'postcode' => ['sometimes', 'string',  'postal_code:' . ($this->currentCountry ?
55                $this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes()))],
56            'opt_in' => 'required|boolean',
57        ];
58    }
59
60    /**
61     * Prepare the data for validation.
62     *
63     * @return void
64     */
65    protected function prepareForValidation(): void
66    {
67        try {
68            if (is_array($this->country)) {
69                $this->currentCountry = Country::make(\intval($this->country));
70            }
71        } catch (\TypeError | \BadMethodCallException $e) {
72            $this->currentCountry = null;
73        }
74        parent::prepareForValidation();
75    }
76
77    /**
78     * Messages
79     *
80     * @return array
81     */
82    public function messages()
83    {
84        return [
85            // phpcs:ignore Generic.Files.LineLength.TooLong
86            'phone.phone' => trans('Le champ :attribute est invalide, il accepte les numéros des pays suivants :countries.', [
87                'countries' => implode(',', Country::getValidCountriesLabel())
88            ]),
89            // phpcs:ignore Generic.Files.LineLength.TooLong
90            'country.in' => trans('Le champ :attribute est invalide, il accepte les pays suivants :countries.', [
91                'countries' => implode(',', Country::getValidCountriesLabel())
92            ])
93        ];
94    }
95}