Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateCustomerRequest
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 rules
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
5
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 UpdateCustomerRequest extends StoreCustomerRequest
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:' . (
45                ($this->currentCountry && $this->currentCountry->get('iso')) ?
46                $this->currentCountry->get('iso') : implode(',', Country::getValidIsoAplha2Codes())
47            ), 'max:255'],
48            'country'    => [
49                $atLeast('country'),
50                'numeric',
51                new Enum(Country::class),
52                sprintf(
53                    'in:%s',
54                    implode(',', Country::getValidCountriesValues())
55                )
56            ],
57            'city'       => [$atLeast('city'), 'string', 'min:2', 'max:255'],
58            'postcode'   => [$atLeast('postcode'), 'string', 'postal_code:' . (
59                ($this->currentCountry && $this->currentCountry->get('iso')) ?
60                $this->currentCountry->get('iso') : implode(',', Country::getValidIsoAplha2Codes())
61            ), 'max:255'],
62            'opt_in'     => [$atLeast('opt_in'), 'boolean'],
63        ];
64    }
65}