Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
16 / 28
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
DateRangeRequestTrait
57.14% covered (warning)
57.14%
16 / 28
50.00% covered (danger)
50.00%
2 / 4
8.83
0.00% covered (danger)
0.00%
0 / 1
 dateRangeRules
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 dateTimeRangeRules
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 validateDateRange
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
2.26
 validateDateTimeRange
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
4.05
1<?php
2
3namespace Domain\Shared\Traits;
4
5use Carbon\Carbon;
6use Closure;
7
8/**
9 * Trait DateRangeTrait
10 *
11 * Provides functionality for generating validation rules for a date range
12 * and validating that the date range does not exceed a specified number of days.
13 */
14trait DateRangeRequestTrait
15{
16    /**
17     * Generate validation rules for a date range.
18     *
19     * @param int $maxDays Maximum number of days allowed in the date range.
20     * @return array Validation rules for the date range.
21     */
22    private function dateRangeRules(int $maxDays = 31): array
23    {
24        return [
25            'nullable', 'array', 'min:2', 'max:2',
26            $this->validateDateRange($maxDays),
27        ];
28    }
29
30    /**
31     * Generate validation rules for a date range.
32     *
33     * @param int $maxDays Maximum number of days allowed in the date range.
34     * @return array Validation rules for the date range.
35     */
36    private function dateTimeRangeRules(int $maxDays = 31): array
37    {
38        return [
39            'nullable', 'array', 'min:2', 'max:2',
40            $this->validateDateTimeRange($maxDays),
41        ];
42    }
43
44    /**
45     * Validates that the date range provided does not exceed a maximum number of days.
46     *
47     * @param int $maxDays The maximum number of days allowed in the date range.
48     * @return Closure The closure function to be used for validation.
49     */
50    private function validateDateRange(int $maxDays = 31): Closure
51    {
52        return function ($attribute, $value, $fail) use ($maxDays) {
53            $dates = array_map(fn($date) => Carbon::createFromFormat('d/m/Y', $date), $value);
54            $minDate = min($dates);
55            $maxDate = max($dates);
56
57            if ($minDate->diffInDays($maxDate) > $maxDays) {
58                $fail(__('The :attribute range cannot exceed :days days.', [
59                    'attribute' => $attribute,
60                    'days' => $maxDays,
61                ]));
62            }
63        };
64    }
65
66    /**
67     * Validates that the date range provided does not exceed a maximum number of days.
68     *
69     * @param int $maxDays The maximum number of days allowed in the date range.
70     * @return Closure The closure function to be used for validation.
71     */
72    private function validateDateTimeRange(int $maxDays = 31): Closure
73    {
74        return function ($attribute, $value, $fail) use ($maxDays) {
75            $dates = array_map(fn($date) => Carbon::createFromFormat('d/m/Y H:i:s', $date), $value);
76            $minDate = min($dates);
77            $maxDate = max($dates);
78
79            if ($minDate->diffInDays($maxDate) > $maxDays) {
80                $fail(__('The :attribute range cannot exceed :days days.', [
81                    'attribute' => $attribute,
82                    'days' => $maxDays,
83                ]));
84            }
85        };
86    }
87}