Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AddressDTO
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 fromRequest
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Domain\Address\DTOs;
4
5use App\Models\Address\Address;
6use Domain\Address\Enums\AddressCategoryEnum;
7use Domain\Shared\Enums\SectorEnum;
8use Domain\Shared\Interfaces\DataTransferObjectInterface;
9use Illuminate\Foundation\Http\FormRequest;
10
11readonly final class AddressDTO implements DataTransferObjectInterface
12{
13    private string $name;
14    public bool $canStore;
15    public bool $canRemove;
16    public bool $status;
17    public AddressCategoryEnum $category;
18    public int $order;
19    public ?Address $parent;
20    public ?SectorEnum $addressSectorEnum;
21
22    public function __construct(array $args)
23    {
24        $this->name = $args['name'];
25        $this->canStore = $args['can_store'];
26        $this->canRemove = $args['can_remove'];
27        $this->status = $args['status'];
28        $this->order = $args['order'];
29        $this->category = $args['category'];
30        $this->parent = $args['parent'] ?? null;
31        $this->addressSectorEnum = $args['sector'] ?? null;
32    }
33
34    public function toArray(): array
35    {
36        return [
37            'name' => $this->name,
38            'can_store' => $this->canStore,
39            'can_remove' => $this->canRemove,
40            'status' => $this->status,
41            'order' => $this->order,
42            'parent_id' => $this->parent?->getKey(),
43            'sector' => $this->addressSectorEnum?->value,
44            'category' => $this->category->value,
45        ];
46
47    }
48
49    public static function fromRequest(FormRequest $request): AddressDTO
50    {
51        return new self([
52            'name' => $request->validated('name'),
53            'can_store' => $request->validated('can_store'),
54            'can_remove' => $request->validated('can_remove'),
55            'status' => $request->validated('status'),
56            'parent' => Address::find($request->validated('parent_id')),
57            'order' => $request->validated('order'),
58            'sector' => SectorEnum::tryFrom($request->validated('sector')),
59            'category' => AddressCategoryEnum::tryFrom($request->validated('category')),
60        ]);
61    }
62}