-
Notifications
You must be signed in to change notification settings - Fork 91
/
Money.php
244 lines (207 loc) · 6.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
declare(strict_types=1);
namespace Shopsys\FrameworkBundle\Component\Money;
use JsonSerializable;
use Litipk\BigNumbers\Decimal;
use Shopsys\FrameworkBundle\Component\Money\Exception\UnsupportedTypeException;
final class Money implements JsonSerializable
{
/**
* @var \Litipk\BigNumbers\Decimal
*/
protected $decimal;
/**
* @param \Litipk\BigNumbers\Decimal $decimal
*/
protected function __construct(Decimal $decimal)
{
$this->decimal = $decimal;
}
/**
* @param int|string $value
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public static function create($value): self
{
$decimal = self::createDecimal($value);
return new self($decimal);
}
/**
* @param float $float
* @param int $scale must be specified when creating from floats to avoid issues with precision
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public static function createFromFloat(float $float, int $scale): self
{
// Using Decimal::fromString as the Decimal::fromFloat has issues with specified scale
// See https://github.com/Litipk/php-bignumbers/pull/67 for details
$decimal = self::createDecimal((string)$float, $scale);
return new self($decimal);
}
/**
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public static function zero(): self
{
return self::create(0);
}
/**
* @return string
*/
public function getAmount(): string
{
if ($this->decimal->isZero() && $this->decimal->isNegative()) {
return \substr((string)$this->decimal, 1);
}
return (string)$this->decimal;
}
/**
* @return string[]
*/
public function jsonSerialize(): array
{
return [
'amount' => $this->getAmount(),
];
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public function add(self $money): self
{
$resultDecimal = $this->decimal->add($money->decimal);
return new self($resultDecimal);
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public function subtract(self $money): self
{
$resultDecimal = $this->decimal->sub($money->decimal);
return new self($resultDecimal);
}
/**
* @param int|string $multiplier
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public function multiply($multiplier): self
{
$decimalMultiplier = self::createDecimal($multiplier);
$resultDecimal = $this->decimal->mul($decimalMultiplier);
return new self($resultDecimal);
}
/**
* @param int|string $divisor
* @param int $scale
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public function divide($divisor, int $scale): self
{
$decimalDivisor = self::createDecimal($divisor);
// Decimal internally ignores scale when number is zero
if ($this->decimal->isZero()) {
return $this->round($scale);
}
$resultDecimal = $this->decimal->div($decimalDivisor, $scale);
return new self($resultDecimal);
}
/**
* @param int $scale
* @return \Shopsys\FrameworkBundle\Component\Money\Money
*/
public function round(int $scale): self
{
$decimal = $this->decimal->round($scale);
return new self($decimal);
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return bool
*/
public function equals(self $money): bool
{
return $this->decimal->equals($money->decimal);
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return int same as spaceship operator (<=>)
*/
public function compare(self $money): int
{
return $this->decimal->comp($money->decimal);
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return bool
*/
public function isGreaterThan(self $money): bool
{
return $this->decimal->isGreaterThan($money->decimal);
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return bool
*/
public function isGreaterThanOrEqualTo(self $money): bool
{
return $this->decimal->isGreaterOrEqualTo($money->decimal);
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return bool
*/
public function isLessThan(self $money): bool
{
return $this->decimal->isLessThan($money->decimal);
}
/**
* @param \Shopsys\FrameworkBundle\Component\Money\Money $money
* @return bool
*/
public function isLessThanOrEqualTo(self $money): bool
{
return $this->decimal->isLessOrEqualTo($money->decimal);
}
/**
* @return bool
*/
public function isNegative(): bool
{
return $this->decimal->isNegative() && !$this->decimal->isZero();
}
/**
* @return bool
*/
public function isPositive(): bool
{
return $this->decimal->isPositive();
}
/**
* @return bool
*/
public function isZero(): bool
{
return $this->decimal->isZero();
}
/**
* @param int|string $value
* @param int|null $scale
* @return \Litipk\BigNumbers\Decimal
*/
protected static function createDecimal($value, ?int $scale = null): Decimal
{
if (is_int($value)) {
return Decimal::fromInteger($value);
}
if (is_string($value)) {
try {
return Decimal::fromString($value, $scale);
} catch (\Litipk\BigNumbers\Errors\BigNumbersError | \InvalidArgumentException $e) {
throw new \Shopsys\FrameworkBundle\Component\Money\Exception\InvalidNumericArgumentException($value, $e);
}
}
throw new UnsupportedTypeException($value, ['string', 'int']);
}
}