1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace hiqdev\php\billing\price; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use hiqdev\php\units\Unit; |
9
|
|
|
use Money\Money; |
10
|
|
|
use hiqdev\php\units\Quantity; |
11
|
|
|
|
12
|
|
|
class ProgressivePriceThreshold |
13
|
|
|
{ |
14
|
|
|
public string $price; |
15
|
|
|
|
16
|
|
|
public string $currency; |
17
|
|
|
|
18
|
|
|
public string $quantity; |
19
|
|
|
|
20
|
|
|
public string $unit; |
21
|
|
|
|
22
|
|
|
private function __construct(string $price, string $currency, string $quantity, string $unit) |
23
|
|
|
{ |
24
|
|
|
if ($quantity < 0) { |
25
|
|
|
throw new InvalidArgumentException('Quantity of the progressive price threshold must be positive'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->price = $price; |
29
|
|
|
$this->currency = strtoupper($currency); |
30
|
|
|
$this->quantity = $quantity; |
31
|
|
|
$this->unit = $unit; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function createFromScalar(string $price, string $currency, string $quantity, string $unit): self |
35
|
|
|
{ |
36
|
|
|
return new self($price, $currency, $quantity, $unit); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function createFromObjects(Money $price, Quantity $quantity) |
40
|
|
|
{ |
41
|
|
|
return new self( |
42
|
|
|
$price->getAmount(), |
43
|
|
|
$price->getCurrency()->getCode(), |
44
|
|
|
$quantity->getQuantity(), |
45
|
|
|
$quantity->getUnit()->getName() |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function price(): ?Money |
50
|
|
|
{ |
51
|
|
|
return MoneyBuilder::buildMoney($this->price, $this->currency); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function quantity(): Quantity |
55
|
|
|
{ |
56
|
|
|
return Quantity::create(Unit::create($this->unit), $this->quantity); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getBasePrice(): string |
60
|
|
|
{ |
61
|
|
|
return $this->price; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __toArray(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'price' => $this->price, |
68
|
|
|
'currency' => $this->currency, |
69
|
|
|
'quantity' => $this->quantity, |
70
|
|
|
'unit' => $this->unit, |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|