Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.50% covered (warning)
62.50%
10 / 16
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddressPermissionDTO
62.50% covered (warning)
62.50%
10 / 16
66.67% covered (warning)
66.67%
2 / 3
3.47
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 toArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 fromRequest
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Domain\Address\DTOs;
4
5use App\Models\Category\Family;
6use App\Models\Category\Origin;
7use App\Models\Product\Product;
8use Domain\Shared\Interfaces\DataTransferObjectInterface;
9use Illuminate\Foundation\Http\FormRequest;
10use Illuminate\Support\Collection;
11
12readonly final class AddressPermissionDTO implements DataTransferObjectInterface
13{
14    public Collection $families;
15    public Collection $origins;
16    public Collection $products;
17    public array $types;
18
19    public function __construct(array $args)
20    {
21        $this->families = Family::query()->whereIn('id', $args['families'])->get();
22        $this->origins = Origin::query()->whereIn('id', $args['origins'])->get();
23        $this->products = Product::query()->whereIn('id', $args['products'])->get();
24        $this->types = $args['types'] ?? [];
25    }
26
27    public function toArray(): array
28    {
29        return [
30            'families' => $this->families->toArray(),
31            'origins' => $this->origins->toArray(),
32            'products' => $this->products->toArray(),
33            'types' => $this->types,
34        ];
35
36    }
37
38    public static function fromRequest(FormRequest $request): AddressPermissionDTO
39    {
40        return new self([
41            'families' => $request->validated('families'),
42            'origins' => $request->validated('origins'),
43            'products' => $request->validated('products'),
44            'types' => $request->validated('types'),
45        ]);
46    }
47}