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