189 lines
7.2 KiB
PHP
189 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Timeslots;
|
|
|
|
use App\Enums\TimeSlot\WeekDay;
|
|
use Carbon\CarbonInterval;
|
|
use Carbon\Exceptions\InvalidFormatException;
|
|
use Carbon\Exceptions\ParseErrorException;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class UpdateTimeslotRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return \auth('backend')->check();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'plages' => \collect($this->plages)->map(function ($shouldbeArray) {
|
|
$shouldbeArray = \collect($shouldbeArray);
|
|
// * Set 0 as default for 'max_orders_per_interval'.
|
|
$shouldbeArray->put('max_orders_per_interval', $shouldbeArray->get('max_orders_per_interval') ?? 0);
|
|
return $shouldbeArray->all();
|
|
})->all()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'plages' => 'required|array',
|
|
'plages.*' => 'required|array',
|
|
'plages.*.morning_start' =>
|
|
'nullable|required_with:plages.*.morning_end|date_format:H:i',
|
|
'plages.*.morning_end' =>
|
|
'nullable|required_with:plages.*.morning_start|date_format:H:i|after:plages.*.morning_start',
|
|
'plages.*.afternoon_start' =>
|
|
'nullable|required_with:plages.*.afternoon_end|date_format:H:i|after:plages.*.morning_end',
|
|
'plages.*.afternoon_end' =>
|
|
'nullable|required_with:plages.*.afternoon_start|date_format:H:i|after:plages.*.afternoon_start',
|
|
'plages.*.interval' => [
|
|
'nullable', 'date_format:H:i',
|
|
'required_with:plages.*.morning_end',
|
|
'required_with:plages.*.morning_start',
|
|
'required_with:plages.*.afternoon_end',
|
|
'required_with:plages.*.afternoon_start',
|
|
$this->validateIntervalMaxValue()
|
|
],
|
|
'plages.*.max_orders_per_interval' => 'nullable|required_with:plages.*.interval|numeric|min:0',
|
|
'plages.*.min_order_preparation' => [
|
|
'nullable',
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'required_with_all:plages.*.morning_start,plages.*.morning_end,plages.*.afternoon_start,plages.*.afternoon_end',
|
|
'regex:/[0-9]{2}\:[0-9]{2}/',
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
try {
|
|
$interval = CarbonInterval::createFromFormat('h:i', $value);
|
|
} catch (ParseErrorException $e) {
|
|
$interval = null;
|
|
}
|
|
// * 6 jours max pour un interval.
|
|
if ($interval and $interval->totalMinutes > (6 * 24 * 60)) {
|
|
$fail(':attribute est limité à 6 jours soit 144 heures');
|
|
return;
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes()
|
|
{
|
|
$attributes = [
|
|
'morning_start' => trans('Matinée Début'),
|
|
'morning_end' => trans('Matinée Fin'),
|
|
'afternoon_start' => trans('Après-midi Début'),
|
|
'afternoon_end' => trans('Après-midi Fin '),
|
|
'interval' => trans('Durée d\'un crénaux'),
|
|
'max_orders_per_interval' => trans('Nb de réservations maximum par crénau'),
|
|
'min_order_preparation' => trans('Delai de préparation de commande')
|
|
];
|
|
return \collect(WeekDay::toArray())->mapWithKeys(function (WeekDay $weekDay) use ($attributes) {
|
|
return \collect($attributes)
|
|
->mapWithKeys(function (string $label, string $attribute) use ($weekDay) {
|
|
return [
|
|
"plages.{$weekDay->value}.{$attribute}" => "{$weekDay->label} : {$label}"
|
|
];
|
|
})->all();
|
|
})->all();
|
|
}
|
|
|
|
/**
|
|
* get validateIntervalMaxValue rule.
|
|
*
|
|
* @return \Closure
|
|
*/
|
|
private function validateIntervalMaxValue(): \Closure
|
|
{
|
|
return function ($attribute, $value, $fail) {
|
|
\preg_match('/^plages\.([1-7])\.interval$/', $attribute, $index);
|
|
if (!isset($index[1]) or !\is_numeric($index[1])) {
|
|
$fail(\trans(":attribute est obligatoire."));
|
|
return;
|
|
}
|
|
$morning_start = null;
|
|
$afternoon_end = null;
|
|
$afternoon_start = null;
|
|
$interval = null;
|
|
|
|
$morning_start = $this->parseTime($this->{"plages.{$index[1]}.morning_start"});
|
|
$morning_end = $this->parseTime($this->{"plages.{$index[1]}.morning_end"});
|
|
$afternoon_start = $this->parseTime($this->{"plages.{$index[1]}.afternoon_start"});
|
|
$afternoon_end = $this->parseTime($this->{"plages.{$index[1]}.afternoon_end"});
|
|
try {
|
|
$interval = CarbonInterval::createFromFormat('h:i', $value);
|
|
} catch (ParseErrorException $e) {
|
|
// * Ignore `interval` if null.
|
|
return;
|
|
}
|
|
|
|
// * Verifier que `interval` peut passer dans une plage.
|
|
if ($morning_start and $morning_end) {
|
|
if ($interval->isEmpty()) {
|
|
$fail(\trans('La valeur de :attribute ne peut être vide.'));
|
|
return;
|
|
}
|
|
// * Exclude _end as a valid interval.
|
|
if (!($morning_start->toPeriod($morning_end, $interval)->count() - 1)) {
|
|
$fail(\trans('La valeur de :attribute est trop élevé pour la plage du matin'));
|
|
return;
|
|
}
|
|
}
|
|
// * Verifier que `interval` peut passer dans une plage.
|
|
if ($afternoon_start and $afternoon_end and !$interval->isEmpty()) {
|
|
if ($interval->isEmpty()) {
|
|
$fail(\trans('La valeur de :attribute ne peut être vide.'));
|
|
return;
|
|
}
|
|
// * Exclude _end as a valid interval.
|
|
if (!($afternoon_start->toPeriod($afternoon_end, $interval)->count() - 1)) {
|
|
$fail(\trans('La valeur de :attribute est trop élevé pour la plage de l\'après-midi'));
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Parse time
|
|
*
|
|
* @param null|string $time
|
|
* @return \Illuminate\Support\Carbon
|
|
*/
|
|
private function parseTime(?string $time): ?Carbon
|
|
{
|
|
try {
|
|
if (!$time) {
|
|
return null;
|
|
}
|
|
return Carbon::parse($time);
|
|
} catch (InvalidFormatException $e) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|