ldc-legaulois/app/Http/Requests/UpdateInfosRequest.php
2022-02-10 16:14:58 +01:00

88 lines
2.9 KiB
PHP

<?php
namespace App\Http\Requests;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
use LDC\LDCCRM\Enums\Country;
class UpdateInfosRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
$this->merge([
'birth_date' => sprintf(
'%d-%s-%s',
$this->year,
str_pad($this->month, 2, '0', \STR_PAD_LEFT),
str_pad($this->day, 2, '0', \STR_PAD_LEFT),
),
'opt_in' => $this->optin ? true : false
]);
return Auth::guard('customer')->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'firstname' => 'required|string|min:1|max:255',
'lastname' => 'required|string|min:1|max:255',
'day' => 'required|numeric|gt:0|lt:32',
'month' => 'required|numeric|gt:0|lt:13',
'year' => [
'required',
'numeric',
'gt:' . (date('Y') - 101), 'lt:' . (date('Y') - 12),
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attr, $value, $fail) {
$dateTest = Carbon::createFromDate($this->year, $this->month, $this->day);
if (
$dateTest->format('dmY') !== $this->day . $this->month . $this->year ||
$dateTest->getTimestamp() > time()
) {
$fail(__('Veuillez entrer une date de naissance valide'));
}
}
],
'birth_date' => 'required|date_format:Y-m-d',
'address' => 'required|min:2|max:255',
'phone' => 'sometimes|max:255',
'city' => 'required|string|min:2|max:255',
'country' => [
'required', new EnumIsValidRule(Country::class)
],
'postcode' => 'required|numeric|digits_between:4,5',
'opt_in' => 'required|boolean'
];
}
public function attributes()
{
return [
'postcode' => trans('Code postal')
];
}
public function messages()
{
return [
// phpcs:ignore Generic.Files.LineLength.TooLong
'postcode.postal_code' => trans('Le champ :attribute accepte les codes postaux des pays suivants :countries comme par exemple :examples.'),
// phpcs:ignore Generic.Files.LineLength.TooLong
'phone.phone' => trans('Le champ :attribute est invalide, il accepte les numéros des pays suivants France, Belgique, Luxembourg.')
];
}
}