Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.64% |
39 / 56 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| PickingOrderSearchDTO | |
69.64% |
39 / 56 |
|
50.00% |
2 / 4 |
8.37 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
| fromRequest | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
2 | |||
| processDate | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
4.68 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\PickingOrder\DTOs; |
| 4 | |
| 5 | use App\Models\User; |
| 6 | use Carbon\Carbon; |
| 7 | use Domain\PickingOrder\Enums\PickingOrderSearchDateEnum; |
| 8 | use Domain\Shared\Interfaces\DataTransferObjectInterface; |
| 9 | use Illuminate\Foundation\Http\FormRequest; |
| 10 | use Illuminate\Support\Collection; |
| 11 | use function count; |
| 12 | |
| 13 | readonly final class PickingOrderSearchDTO implements DataTransferObjectInterface |
| 14 | { |
| 15 | public ?PickingOrderSearchDateEnum $pickingOrderSearchDateEnum; |
| 16 | |
| 17 | /** @var null|Carbon[] */ |
| 18 | public ?array $startDate; |
| 19 | public ?array $endDate; |
| 20 | public ?array $createdDate; |
| 21 | |
| 22 | public Collection $responsible; |
| 23 | public Collection $customer; |
| 24 | public Collection $status; |
| 25 | public ?array $preInvoice; |
| 26 | public ?int $id; |
| 27 | public ?int $stockIOId; |
| 28 | public ?int $type; |
| 29 | |
| 30 | public function __construct(array $args) |
| 31 | { |
| 32 | $this->id = $args['id'] ?? null; |
| 33 | $this->stockIOId = $args['stock_io_id'] ?? null; |
| 34 | $this->pickingOrderSearchDateEnum = $args['date_range']; |
| 35 | $this->startDate = $args['start_date']; |
| 36 | $this->endDate = $args['end_date']; |
| 37 | $this->createdDate = $args['created_date']; |
| 38 | $this->customer = $args['customer']; |
| 39 | $this->status = $args['status']; |
| 40 | $this->preInvoice = $args['pre_invoice']; |
| 41 | $this->responsible = $args['responsible']; |
| 42 | $this->type = $args['type'] ?? null; |
| 43 | } |
| 44 | |
| 45 | public function toArray(): array |
| 46 | { |
| 47 | return [ |
| 48 | 'id' => $this->id, |
| 49 | 'stock_io_id' => $this->stockIOId, |
| 50 | 'date_range' => $this->pickingOrderSearchDateEnum?->value, |
| 51 | 'start_date' => $this->startDate, |
| 52 | 'end_date' => $this->endDate, |
| 53 | 'created_date' => $this->createdDate, |
| 54 | 'responsible' => $this->responsible, |
| 55 | 'customer' => $this->customer, |
| 56 | 'pre_invoice' => $this->preInvoice, |
| 57 | 'status' => $this->status, |
| 58 | 'type' => $this->type, |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | public static function fromRequest(FormRequest $request): PickingOrderSearchDTO |
| 63 | { |
| 64 | $startDate = self::processDate($request->validated('start_date')); |
| 65 | $endDate = self::processDate($request->validated('end_date')); |
| 66 | $createdDate = self::processDate($request->validated('created_date')); |
| 67 | $pickingOrderSearchDateEnum = $request->validated('date_range') ? |
| 68 | PickingOrderSearchDateEnum::tryFrom($request->validated('date_range')) : null; |
| 69 | $customer = new Collection($request->validated('customer') ?? []); |
| 70 | $status = new Collection($request->validated('status') ?? []); |
| 71 | $preInvoice = $request->validated('pre_invoice'); |
| 72 | $responsible = new Collection($request->validated('responsible') ?? []); |
| 73 | $id = $request->validated('id'); |
| 74 | $stockIOId = $request->validated('stock_io_id'); |
| 75 | $type = $request->validated('type'); |
| 76 | |
| 77 | return new self([ |
| 78 | 'id' => $id, |
| 79 | 'stock_io_id' => $stockIOId, |
| 80 | 'date_range' => $pickingOrderSearchDateEnum, |
| 81 | 'start_date' => $startDate, |
| 82 | 'end_date' => $endDate, |
| 83 | 'created_date' => $createdDate, |
| 84 | 'customer' => $customer, |
| 85 | 'status' => $status, |
| 86 | 'pre_invoice' => $preInvoice, |
| 87 | 'responsible' => $responsible, |
| 88 | 'type' => $type, |
| 89 | ]); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Processes a date range and returns an array of Carbon instances. |
| 94 | * |
| 95 | * If a valid date range is provided as an array with two elements, this function will convert each date |
| 96 | * in the range from a string in the format "d/m/Y" to a Carbon instance and add it to the result array. |
| 97 | * |
| 98 | * If an invalid or empty date range is provided, an empty array will be returned. |
| 99 | * |
| 100 | * @param array|null $dateRange The date range to process. |
| 101 | * @return ?array The processed dates as an array of Carbon |
| 102 | */ |
| 103 | private static function processDate(?array $dateRange): ?array |
| 104 | { |
| 105 | $dates = null; |
| 106 | if ($dateRange && count($dateRange) == 2) { |
| 107 | $dates = [ |
| 108 | Carbon::createFromFormat('d/m/Y', $dateRange[0]), |
| 109 | Carbon::createFromFormat('d/m/Y', $dateRange[1]) |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | return $dates; |
| 114 | } |
| 115 | } |