2021-03-14 10:45:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
2021-07-28 14:18:47 +02:00
|
|
|
namespace App\Http\Requests\Api\V2;
|
2021-03-14 10:45:42 +01:00
|
|
|
|
|
|
|
|
use App\Enums\Country;
|
2021-08-06 15:57:48 +02:00
|
|
|
use Illuminate\Validation\Rules\Password;
|
2021-10-20 15:30:21 +02:00
|
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
2021-03-14 10:45:42 +01:00
|
|
|
|
2022-06-09 11:09:11 +02:00
|
|
|
class UpdateCustomerRequest extends StoreCustomerRequest
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
$fields = [
|
2021-03-14 10:45:42 +01:00
|
|
|
'firstname', 'lastname', 'email', 'password',
|
|
|
|
|
'birth_date', 'address', 'phone', 'country',
|
|
|
|
|
'city', 'postcode', 'opt_in'
|
|
|
|
|
];
|
2021-12-19 15:57:24 +01:00
|
|
|
$atLeast = function ($attribute) use ($fields) {
|
|
|
|
|
return 'required_without_all:' . collect($fields)
|
|
|
|
|
->filter(function ($fieldName) use ($attribute) {
|
|
|
|
|
return $fieldName !== $attribute;
|
|
|
|
|
})->implode(',');
|
2021-03-14 10:45:42 +01:00
|
|
|
};
|
|
|
|
|
return [
|
2021-12-19 15:57:24 +01:00
|
|
|
'firstname' => [$atLeast('firstname'), 'string', 'min:1', 'max:255'],
|
|
|
|
|
'lastname' => [$atLeast('lastname'), 'string', 'min:1', 'max:255'],
|
|
|
|
|
'email' => [$atLeast('email'), 'email:rfc,dns,spoof,filter', 'max:255'],
|
2021-08-06 15:57:48 +02:00
|
|
|
'password' => [
|
2021-12-19 15:57:24 +01:00
|
|
|
$atLeast('password'), 'max:255', Password::min(8)
|
2021-08-06 15:57:48 +02:00
|
|
|
->mixedCase()
|
|
|
|
|
->letters()
|
|
|
|
|
->numbers()
|
|
|
|
|
->symbols()
|
|
|
|
|
->uncompromised()
|
|
|
|
|
],
|
2021-12-19 15:57:24 +01:00
|
|
|
'birth_date' => [$atLeast('birth_date'), 'date_format:Y-m-d', 'max:255'],
|
|
|
|
|
'address' => [$atLeast('address'), 'required_with:address2', 'string', 'max:255'],
|
2021-10-20 15:30:21 +02:00
|
|
|
'address2' => ['sometimes', 'string', 'max:255'],
|
2022-06-09 11:09:11 +02:00
|
|
|
'phone' => [$atLeast('phone'), 'string', 'phone:' . ($this->currentCountry ?
|
|
|
|
|
$this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes())), 'max:255'],
|
2021-03-14 10:45:42 +01:00
|
|
|
'country' => [
|
2021-12-19 15:57:24 +01:00
|
|
|
$atLeast('country'),
|
2021-03-14 10:45:42 +01:00
|
|
|
'numeric',
|
2021-10-20 15:30:21 +02:00
|
|
|
new EnumIsValidRule(Country::class),
|
|
|
|
|
sprintf(
|
|
|
|
|
'in:%s',
|
2022-06-09 11:09:11 +02:00
|
|
|
implode(',', Country::getValidCountries())
|
2021-10-20 15:30:21 +02:00
|
|
|
)
|
2021-03-14 10:45:42 +01:00
|
|
|
],
|
2021-12-19 15:57:24 +01:00
|
|
|
'city' => [$atLeast('city'), 'string', 'min:2', 'max:255'],
|
2022-06-09 11:09:11 +02:00
|
|
|
'postcode' => [$atLeast('postcode'), 'string', 'postal_code:' . ($this->currentCountry ?
|
|
|
|
|
$this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes())), 'max:255'],
|
2021-12-19 15:57:24 +01:00
|
|
|
'opt_in' => [$atLeast('opt_in'), 'boolean'],
|
2021-03-14 10:45:42 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|