ldc-crm/app/Http/Requests/Api/V2/UpdateCustomerRequest.php
Jérémy Munsch f2371d97ea
Fixed #905
- handled Country and Phone on customer store and update
- refactorize Country and Phone
2022-06-09 11:09:11 +02:00

61 lines
2.4 KiB
PHP

<?php
namespace App\Http\Requests\Api\V2;
use App\Enums\Country;
use Illuminate\Validation\Rules\Password;
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
class UpdateCustomerRequest extends StoreCustomerRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
$fields = [
'firstname', 'lastname', 'email', 'password',
'birth_date', 'address', 'phone', 'country',
'city', 'postcode', 'opt_in'
];
$atLeast = function ($attribute) use ($fields) {
return 'required_without_all:' . collect($fields)
->filter(function ($fieldName) use ($attribute) {
return $fieldName !== $attribute;
})->implode(',');
};
return [
'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'],
'password' => [
$atLeast('password'), 'max:255', Password::min(8)
->mixedCase()
->letters()
->numbers()
->symbols()
->uncompromised()
],
'birth_date' => [$atLeast('birth_date'), 'date_format:Y-m-d', 'max:255'],
'address' => [$atLeast('address'), 'required_with:address2', 'string', 'max:255'],
'address2' => ['sometimes', 'string', 'max:255'],
'phone' => [$atLeast('phone'), 'string', 'phone:' . ($this->currentCountry ?
$this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes())), 'max:255'],
'country' => [
$atLeast('country'),
'numeric',
new EnumIsValidRule(Country::class),
sprintf(
'in:%s',
implode(',', Country::getValidCountries())
)
],
'city' => [$atLeast('city'), 'string', 'min:2', 'max:255'],
'postcode' => [$atLeast('postcode'), 'string', 'postal_code:' . ($this->currentCountry ?
$this->currentCountry->iso : implode(',', Country::getValidIsoAplha2Codes())), 'max:255'],
'opt_in' => [$atLeast('opt_in'), 'boolean'],
];
}
}