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