|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace byrokrat\amount; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Abstract base currency class |
|
9
|
|
|
* |
|
10
|
|
|
* Create currency by extending this class. Prevents addition and other operations |
|
11
|
|
|
* on amounts from different currencies. |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Currency extends Amount |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create new currency object by exchange from foreign currency |
|
17
|
|
|
* |
|
18
|
|
|
* @param Currency $amount The amount to exchange |
|
19
|
|
|
* @param int|float|string|Amount $rate The exchange rate used |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public static function createFromExchange(Currency $amount, $rate): Currency |
|
22
|
|
|
{ |
|
23
|
1 |
|
return new static($amount->multiplyWith($rate)->getAmount()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get ISO-4217 currency name |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract public function getCurrencyCode(): string; |
|
30
|
|
|
|
|
31
|
2 |
|
public function add(Amount $amount, int $precision = -1): Amount |
|
32
|
|
|
{ |
|
33
|
2 |
|
return parent::add($this->validateCurrency($amount), $precision); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
public function subtract(Amount $amount, int $precision = -1): Amount |
|
37
|
|
|
{ |
|
38
|
2 |
|
return parent::subtract($this->validateCurrency($amount), $precision); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
8 |
|
public function compareTo(Amount $amount, int $precision = -1): int |
|
42
|
|
|
{ |
|
43
|
8 |
|
return parent::compareTo($this->validateCurrency($amount), $precision); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
public function equals(Amount $amount, int $precision = -1): bool |
|
47
|
|
|
{ |
|
48
|
5 |
|
return parent::equals($this->validateCurrency($amount), $precision); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
public function isLessThan(Amount $amount, int $precision = -1): bool |
|
52
|
|
|
{ |
|
53
|
3 |
|
return parent::isLessThan($this->validateCurrency($amount), $precision); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function isLessThanOrEquals(Amount $amount, int $precision = -1): bool |
|
57
|
|
|
{ |
|
58
|
2 |
|
return parent::isLessThanOrEquals($this->validateCurrency($amount), $precision); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
public function isGreaterThan(Amount $amount, int $precision = -1): bool |
|
62
|
|
|
{ |
|
63
|
3 |
|
return parent::isGreaterThan($this->validateCurrency($amount), $precision); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
public function isGreaterThanOrEquals(Amount $amount, int $precision = -1): bool |
|
67
|
|
|
{ |
|
68
|
2 |
|
return parent::isGreaterThanOrEquals($this->validateCurrency($amount), $precision); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Validate that amount is in the expected currency |
|
73
|
|
|
* |
|
74
|
|
|
* @throws InvalidArgumentException If amount is in an unexpected currency |
|
75
|
|
|
*/ |
|
76
|
17 |
|
protected function validateCurrency(Amount $amount): Amount |
|
77
|
|
|
{ |
|
78
|
17 |
|
if (!$amount instanceof static) { |
|
79
|
8 |
|
throw new InvalidArgumentException( |
|
80
|
8 |
|
"Currency " . get_class($this) . " expected, found " . get_class($amount) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
9 |
|
return $amount; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|