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