diff --git a/app/Http/Requests/Bo/Timeslots/UpdateTimeslotRequest.php b/app/Http/Requests/Bo/Timeslots/UpdateTimeslotRequest.php index c8f1f31..65918a1 100644 --- a/app/Http/Requests/Bo/Timeslots/UpdateTimeslotRequest.php +++ b/app/Http/Requests/Bo/Timeslots/UpdateTimeslotRequest.php @@ -62,53 +62,7 @@ class UpdateTimeslotRequest extends FormRequest 'required_with:plages.*.morning_start', 'required_with:plages.*.afternoon_end', 'required_with:plages.*.afternoon_start', - 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; - } - $start_morning = null; - $morning_start = null; - $afternoon_end = null; - $afternoon_start = null; - $interval = null; - $parseTime = function (string $time) { - try { - return Carbon::parse($time); - } catch (InvalidFormatException $e) { - return null; - } - }; - - $morning_start = $parseTime($this->{"plages.{$index[1]}.morning_start"} ?? ''); - $morning_end = $parseTime($this->{"plages.{$index[1]}.morning_end"} ?? ''); - $afternoon_start = $parseTime($this->{"plages.{$index[1]}.afternoon_start"} ?? ''); - $afternoon_end = $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) { - // * 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) { - // * 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; - } - } - } + $this->validateIntervalMaxValue() ], 'plages.*.max_orders_per_interval' => 'nullable|required_with:plages.*.interval|numeric|min:0', 'plages.*.min_order_preparation' => [ @@ -155,4 +109,59 @@ class UpdateTimeslotRequest extends FormRequest })->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; + $parseTime = function ($time) { + try { + return Carbon::parse(\strval($time)); + } catch (InvalidFormatException $e) { + return null; + } + }; + + $morning_start = $parseTime($this->{"plages.{$index[1]}.morning_start"}); + $morning_end = $parseTime($this->{"plages.{$index[1]}.morning_end"}); + $afternoon_start = $parseTime($this->{"plages.{$index[1]}.afternoon_start"}); + $afternoon_end = $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) { + // * 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) { + // * 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; + } + } + }; + } }