Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaskResumedProgressSearchDTO
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 fromRequest
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 processDate
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Domain\Shared\DTOs;
4
5use Carbon\Carbon;
6use Domain\Shared\Interfaces\DataTransferObjectInterface;
7use Illuminate\Foundation\Http\FormRequest;
8use function count;
9
10readonly final class TaskResumedProgressSearchDTO implements DataTransferObjectInterface
11{
12    /** @var null|Carbon[] */
13    public ?array $date_range;
14
15    public function __construct(array $args)
16    {
17        $this->date_range = $args['date_range'] ?? [];
18    }
19
20    public function toArray(): array
21    {
22        return [
23            'date_range' => $this->date_range,
24        ];
25    }
26
27    public static function fromRequest(FormRequest $request): TaskResumedProgressSearchDTO
28    {
29        $date_range = self::processDate($request->validated('date_range'));
30
31        return new self([
32            'date_range' => $date_range,
33        ]);
34    }
35
36    /**
37     * Processes a date range and returns an array of Carbon instances.
38     *
39     * If a valid date range is provided as an array with two elements, this function will convert each date
40     * in the range from a string in the format "d/m/Y" to a Carbon instance and add it to the result array.
41     *
42     * If an invalid or empty date range is provided, an empty array will be returned.
43     *
44     * @param array|null $dateRange The date range to process.
45     * @return ?array The processed dates as an array of Carbon
46     */
47    private static function processDate(?array $dateRange): ?array
48    {
49        $dates = null;
50        if ($dateRange && count($dateRange) == 2) {
51            $dates = [
52                Carbon::createFromFormat('d/m/Y', $dateRange[0]),
53                Carbon::createFromFormat('d/m/Y', $dateRange[1])
54            ];
55        }
56
57        return $dates;
58    }
59}