-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoney.php
162 lines (134 loc) · 4.37 KB
/
Money.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Money;
use JsonSerializable;
use SonsOfPHP\Component\Money\Operator\Money\AddMoneyOperator;
use SonsOfPHP\Component\Money\Operator\Money\DivideMoneyOperator;
use SonsOfPHP\Component\Money\Operator\Money\MultiplyMoneyOperator;
use SonsOfPHP\Component\Money\Operator\Money\SubtractMoneyOperator;
use SonsOfPHP\Component\Money\Query\Money\IsEqualToMoneyQuery;
use SonsOfPHP\Component\Money\Query\Money\IsGreaterThanMoneyQuery;
use SonsOfPHP\Component\Money\Query\Money\IsGreaterThanOrEqualToMoneyQuery;
use SonsOfPHP\Component\Money\Query\Money\IsLessThanMoneyQuery;
use SonsOfPHP\Component\Money\Query\Money\IsLessThanOrEqualToMoneyQuery;
use SonsOfPHP\Component\Money\Query\Money\IsNegativeMoneyQuery;
use SonsOfPHP\Component\Money\Query\Money\IsPositiveMoneyQuery;
use SonsOfPHP\Component\Money\Query\Money\IsZeroMoneyQuery;
use SonsOfPHP\Contract\Money\AmountInterface;
use SonsOfPHP\Contract\Money\CurrencyInterface;
use SonsOfPHP\Contract\Money\MoneyInterface;
use SonsOfPHP\Contract\Money\MoneyOperatorInterface;
use SonsOfPHP\Contract\Money\MoneyQueryInterface;
use Stringable;
/**
* @author Joshua Estes <[email protected]>
*/
final class Money implements MoneyInterface, JsonSerializable, Stringable
{
private readonly AmountInterface $amount;
public function __construct($amount, private readonly CurrencyInterface $currency)
{
if (!$amount instanceof AmountInterface) {
$amount = new Amount($amount);
}
$this->amount = $amount;
}
public function __toString(): string
{
return $this->amount->toString();
}
/**
* Example: Money::USD(100);.
*/
public static function __callStatic(string $method, array $args)
{
return new self($args[0], new Currency($method));
}
/**
* @return int
* -1 = this less than that
* 0 = this equals that
* 1 = this greater than that
*/
public function compare(MoneyInterface $money): int
{
if ($this->isLessThan($money)) {
return -1;
}
if ($this->isGreaterThan($money)) {
return 1;
}
return 0;
}
public function with(MoneyOperatorInterface $operator): MoneyInterface
{
return $operator->apply($this);
}
public function query(MoneyQueryInterface $query)
{
return $query->queryFrom($this);
}
public function getAmount(): AmountInterface
{
return $this->amount;
}
public function getCurrency(): CurrencyInterface
{
return $this->currency;
}
public function isEqualTo(MoneyInterface $money): bool
{
return $this->query(new IsEqualToMoneyQuery($money));
}
public function isGreaterThan(MoneyInterface $money): bool
{
return $this->query(new IsGreaterThanMoneyQuery($money));
}
public function isGreaterThanOrEqualTo(MoneyInterface $money): bool
{
return $this->query(new IsGreaterThanOrEqualToMoneyQuery($money));
}
public function isLessThan(MoneyInterface $money): bool
{
return $this->query(new IsLessThanMoneyQuery($money));
}
public function isLessThanOrEqualTo(MoneyInterface $money): bool
{
return $this->query(new IsLessThanOrEqualToMoneyQuery($money));
}
public function isNegative(): bool
{
return $this->query(new IsNegativeMoneyQuery());
}
public function isPositive(): bool
{
return $this->query(new IsPositiveMoneyQuery());
}
public function isZero(): bool
{
return $this->query(new IsZeroMoneyQuery());
}
public function add(MoneyInterface $money): MoneyInterface
{
return $this->with(new AddMoneyOperator($money));
}
public function subtract(MoneyInterface $money): MoneyInterface
{
return $this->with(new SubtractMoneyOperator($money));
}
public function multiply($multiplier): MoneyInterface
{
return $this->with(new MultiplyMoneyOperator($multiplier));
}
public function divide($divisor): MoneyInterface
{
return $this->with(new DivideMoneyOperator($divisor));
}
public function jsonSerialize(): array
{
return [
'amount' => $this->getAmount()->toInt(),
'currency' => $this->getCurrency()->getCurrencyCode(),
];
}
}