Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
DateRangeDTOTrait
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 processDate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Domain\Shared\Traits;
4
5use Carbon\Carbon;
6use Closure;
7
8/**
9 * Trait DateRangeDTOTrait
10 *
11 * Contains methods to process date ranges and convert them into Carbon instances.
12 */
13trait DateRangeDTOTrait
14{
15    /**
16     * Processes a date range and returns an array of Carbon instances.
17     *
18     * If a valid date range is provided as an array with two elements, this function will convert each date
19     * in the range from a string in the format "d/m/Y" to a Carbon instance and add it to the result array.
20     *
21     * If an invalid or empty date range is provided, an empty array will be returned.
22     *
23     * @param array|null $dateRange The date range to process.
24     * @return ?array The processed dates as an array of Carbon
25     */
26    private static function processDate(?array $dateRange, string $dateFormat = 'd/m/Y'): ?array
27    {
28        $dates = null;
29        if ($dateRange && count($dateRange) == 2) {
30            $dates = [
31                Carbon::createFromFormat($dateFormat, $dateRange[0]),
32                Carbon::createFromFormat($dateFormat, $dateRange[1])
33            ];
34        }
35
36        return $dates;
37    }
38}