Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| StoreCustomerRequest | |
0.00% |
0 / 25 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| rules | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| prepareForValidation | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| messages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
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 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 | } |