Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ShippingPackagingDTO
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 fromRequest
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Domain\ShippingPackaging\DTOs;
4
5use App\Models\Packaging\Packaging;
6use App\Models\Team\Team;
7use Domain\Shared\Interfaces\DataTransferObjectInterface;
8use Domain\ShippingPackaging\Enums\ShippingPackagingTypeEnum;
9use Illuminate\Foundation\Http\FormRequest;
10
11final readonly class ShippingPackagingDTO implements DataTransferObjectInterface
12{
13    public Packaging $packaging;
14    public ShippingPackagingTypeEnum $type;
15    public ?Team $team;
16    public ?int $volume;
17
18    public function __construct(array $args)
19    {
20        $this->packaging = $args['packaging'];
21        $this->type = $args['type'];
22        $this->team = $args['team'] ?? null;
23        $this->volume = $args['volume'] ?? null;
24    }
25
26    public function toArray(): array
27    {
28        return [
29            'packaging_id' => $this->packaging->getKey(),
30            'type' => $this->type->value,
31            'team_id' => $this->team?->getKey(),
32            'volume' => $this->volume,
33        ];
34    }
35
36    public static function fromRequest(FormRequest $request): ShippingPackagingDTO
37    {
38        return new self([
39            'team' => $request->validated('team_id') ? Team::find($request->validated('team_id')) : null,
40            'packaging' => Packaging::find($request->validated('packaging_id')),
41            'volume' => $request->validated('volume'),
42            'type' => ShippingPackagingTypeEnum::tryFrom($request->validated('type'))
43        ]);
44    }
45}