-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmount.php
72 lines (59 loc) · 1.68 KB
/
Amount.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* @author enea dhack <[email protected]>
*/
declare(strict_types=1);
namespace Vaened\PriceEngine\Money;
use Brick\Money\Money;
use Vaened\PriceEngine\Adjustments\Taxation\TaxCodes;
use Vaened\PriceEngine\Adjustments\Taxation\Taxes;
use function count;
final class Amount implements Priceable
{
private ?Taxes $taxes;
public function __construct(
private readonly Money $unitPrice,
private readonly TaxCodes $applicableTaxCodes,
Taxes $taxes = null,
)
{
$this->ensureCanHaveTaxes($taxes);
$this->taxes = $taxes;
}
public static function taxable(Money $unitPrice, TaxCodes $codes = new TaxCodes(TaxCodes::ANY)): self
{
return new self($unitPrice, applicableTaxCodes: $codes);
}
public static function taxexempt(Money $unitPrice): self
{
return new self($unitPrice, applicableTaxCodes: TaxCodes::none());
}
public function impose(Taxes|array $taxes): self
{
$this->ensureCanHaveTaxes($taxes);
$this->taxes = $taxes instanceof Taxes ? $taxes : Taxes::from($taxes);
return $this;
}
public function value(): Money
{
return $this->unitPrice;
}
public function taxes(): Taxes
{
return $this->taxes ??= Taxes::empty();
}
public function applicableCodes(): TaxCodes
{
return $this->applicableTaxCodes;
}
private function ensureCanHaveTaxes(Taxes|array|null $taxes): void
{
if (
null !== $taxes &&
count($taxes) > 0 &&
$this->applicableCodes()->isNothingAllowed()
) {
throw new NonTaxableItem();
}
}
}