|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodebarAg\Bexio\Dto\Invoices; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Support\Arr; |
| 7 | +use Saloon\Http\Response; |
| 8 | +use Spatie\LaravelData\Data; |
| 9 | + |
| 10 | +class InvoicePositionDTO extends Data |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + public ?string $type, |
| 14 | + public ?int $id, |
| 15 | + public ?string $amount, |
| 16 | + public ?int $unit_id, |
| 17 | + public ?string $unit_name, |
| 18 | + public ?int $account_id, |
| 19 | + public ?int $tax_id, |
| 20 | + public ?string $tax_value, |
| 21 | + public ?string $text, |
| 22 | + public ?string $unit_price, |
| 23 | + public ?string $discount_in_percent, |
| 24 | + public ?string $position_total, |
| 25 | + public ?int $parent_id, |
| 26 | + public ?int $article_id, |
| 27 | + public ?bool $show_pos_nr, |
| 28 | + public ?bool $pagebreak, |
| 29 | + public ?bool $is_percentual, |
| 30 | + public ?string $value, |
| 31 | + public ?string $pos, |
| 32 | + public ?string $internal_pos, |
| 33 | + public ?bool $is_optional, |
| 34 | + ) {} |
| 35 | + |
| 36 | + public static function fromResponse(Response $response): self |
| 37 | + { |
| 38 | + if ($response->failed()) { |
| 39 | + throw new \Exception('Failed to create DTO from Response'); |
| 40 | + } |
| 41 | + |
| 42 | + $data = $response->json(); |
| 43 | + |
| 44 | + return self::fromArray($data); |
| 45 | + } |
| 46 | + |
| 47 | + public static function fromArray(array $data): self |
| 48 | + { |
| 49 | + if (! $data) { |
| 50 | + throw new Exception('Unable to create DTO. Data missing from response.'); |
| 51 | + } |
| 52 | + |
| 53 | + return new self( |
| 54 | + type: Arr::get($data, 'type'), |
| 55 | + id: Arr::get($data, 'id'), |
| 56 | + amount: Arr::get($data, 'amount'), |
| 57 | + unit_id: Arr::get($data, 'unit_id'), |
| 58 | + unit_name: Arr::get($data, 'unit_name'), |
| 59 | + account_id: Arr::get($data, 'account_id'), |
| 60 | + tax_id: Arr::get($data, 'tax_id'), |
| 61 | + tax_value: Arr::get($data, 'tax_value'), |
| 62 | + text: Arr::get($data, 'text'), |
| 63 | + unit_price: Arr::get($data, 'unit_price'), |
| 64 | + discount_in_percent: Arr::get($data, 'discount_in_percent'), |
| 65 | + position_total: Arr::get($data, 'position_total'), |
| 66 | + parent_id: Arr::get($data, 'parent_id'), |
| 67 | + article_id: Arr::get($data, 'article_id'), |
| 68 | + show_pos_nr: Arr::get($data, 'show_pos_nr'), |
| 69 | + pagebreak: Arr::get($data, 'pagebreak'), |
| 70 | + is_percentual: Arr::get($data, 'is_percentual'), |
| 71 | + value: Arr::get($data, 'value'), |
| 72 | + pos: Arr::get($data, 'pos'), |
| 73 | + internal_pos: Arr::get($data, 'internal_pos'), |
| 74 | + is_optional: Arr::get($data, 'is_optional'), |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments