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