Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| TaxGroupDto | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromArray | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Modules\Tax\Application\DTO; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | /** |
| 12 | * Data Transfer Object for creating or updating a TaxGroup. |
| 13 | */ |
| 14 | final readonly class TaxGroupDto |
| 15 | { |
| 16 | /** |
| 17 | * TaxGroupDto constructor. |
| 18 | * |
| 19 | * @param int|null $id |
| 20 | * @param string $groupCode |
| 21 | * @param string $groupName |
| 22 | * @param string|null $description |
| 23 | * @param bool $isActive |
| 24 | * @param array<int, array{tax_rate_id: int, sequence_order?: int, is_compound?: bool}> $items |
| 25 | */ |
| 26 | public function __construct( |
| 27 | public ?int $id, |
| 28 | public string $groupCode, |
| 29 | public string $groupName, |
| 30 | public ?string $description = null, |
| 31 | public bool $isActive = true, |
| 32 | public array $items = [], |
| 33 | ) { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Create DTO from raw array. |
| 38 | * |
| 39 | * @param array<string, mixed> $data |
| 40 | */ |
| 41 | public static function fromArray(array $data, ?int $id = null): self |
| 42 | { |
| 43 | $rawItems = isset($data['items']) && is_array($data['items']) ? $data['items'] : []; |
| 44 | $items = []; |
| 45 | |
| 46 | foreach ($rawItems as $idx => $item) { |
| 47 | if (isset($item['tax_rate_id'])) { |
| 48 | $items[] = [ |
| 49 | 'tax_rate_id' => (int) $item['tax_rate_id'], |
| 50 | 'sequence_order' => (int) ($item['sequence_order'] ?? ($idx + 1)), |
| 51 | 'is_compound' => !empty($item['is_compound']), |
| 52 | ]; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return new self( |
| 57 | id: $id ?? (isset($data['id']) ? (int) $data['id'] : null), |
| 58 | groupCode: trim((string) ($data['group_code'] ?? '')), |
| 59 | groupName: trim((string) ($data['group_name'] ?? '')), |
| 60 | description: !empty($data['description']) ? trim((string) $data['description']) : null, |
| 61 | isActive: isset($data['is_active']) ? (bool) $data['is_active'] : true, |
| 62 | items: $items, |
| 63 | ); |
| 64 | } |
| 65 | } |