Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RequestItemUpdateDTO
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 fromRequest
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 fromArray
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
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 RequestItemUpdateDTO implements DataTransferObjectInterface
12{
13    public int $id;
14    public ?Product $product;
15    public ?float $amount;
16
17    public function __construct(array $args)
18    {
19        $this->id      = $args['id'];
20        $this->product = $args['product'] ?? null;
21        $this->amount  = $args['amount'] ?? null;
22    }
23
24    public function toArray(): array
25    {
26        return [
27            'id'         => $this->id,
28            'product_id' => $this->product ? $this->product->getKey() : null,
29            'amount'     => $this->amount,
30        ];
31    }
32
33    public static function fromRequest(FormRequest $request): RequestItemUpdateDTO
34    {
35        return new self([
36            'id'      => $request->validated('id'),
37            'product' => Product::find($request->validated('product')),
38            'amount'  => $request->validated('amount'),
39        ]);
40    }
41
42    public static function fromArray(array $array): RequestItemUpdateDTO
43    {
44        return new self([
45            'id'      => $array['id'],
46            'product' => isset($array['product']) ? Product::find($array['product']) : null,
47            'amount'  => isset($array['amount']) ? $array['amount'] : null,
48        ]);
49    }
50}