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