Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TaxRateDto
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Modules\Tax\Application\DTO;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Data Transfer Object for creating or updating a TaxRate.
13 */
14final readonly class TaxRateDto
15{
16    /**
17     * TaxRateDto constructor.
18     */
19    public function __construct(
20        public ?int $id,
21        public string $taxCode,
22        public string $taxName,
23        public float $ratePercent,
24        public string $calculationMode = 'exclusive',
25        public string $taxStatus = 'standard',
26        public ?string $legalExemptionNote = null,
27        public ?string $fiscalCode = null,
28        public bool $isDefault = false,
29        public bool $isActive = true,
30    ) {
31    }
32
33    /**
34     * Create DTO from raw array (e.g. from JSON body or POST form data).
35     *
36     * @param array<string, mixed> $data
37     */
38    public static function fromArray(array $data, ?int $id = null): self
39    {
40        return new self(
41            id: $id ?? (isset($data['id']) ? (int) $data['id'] : null),
42            taxCode: trim((string) ($data['tax_code'] ?? '')),
43            taxName: trim((string) ($data['tax_name'] ?? '')),
44            ratePercent: (float) ($data['rate_percent'] ?? 0.0),
45            calculationMode: (string) ($data['calculation_mode'] ?? 'exclusive'),
46            taxStatus: (string) ($data['tax_status'] ?? 'standard'),
47            legalExemptionNote: !empty($data['legal_exemption_note'])
48                ? trim((string) $data['legal_exemption_note']) : null,
49            fiscalCode: !empty($data['fiscal_code']) ? trim((string) $data['fiscal_code']) : null,
50            isDefault: !empty($data['is_default']),
51            isActive: isset($data['is_active']) ? (bool) $data['is_active'] : true,
52        );
53    }
54}