-
Notifications
You must be signed in to change notification settings - Fork 92
/
Money.php
283 lines (250 loc) · 6.85 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace Cknow\Money;
use BadMethodCallException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
/**
* Money.
*
* @method bool isSameCurrency(Money|\Money\Money ...$others)
* @method bool equals(Money|\Money\Money $other)
* @method int compare(Money|\Money\Money $other)
* @method bool greaterThan(Money|\Money\Money $other)
* @method bool greaterThanOrEqual(Money|\Money\Money $other)
* @method bool lessThan(Money|\Money\Money $other)
* @method bool lessThanOrEqual(Money|\Money\Money $other)
* @method string getAmount()
* @method \Money\Currency getCurrency()
* @method Money add(Money|\Money\Money ...$addends)
* @method Money subtract(Money|\Money\Money ...$subtrahends)
* @method Money mod(Money|\Money\Money $divisor)
* @method Money[] allocate(array $ratios)
* @method Money[] allocateTo(int $n)
* @method string ratioOf(Money|\Money\Money $money)
* @method Money absolute()
* @method Money negative()
* @method bool isZero()
* @method bool isPositive()
* @method bool isNegative()
* @method static Money min(Money|\Money\Money $first, Money|\Money\Money ...$collection)
* @method static Money max(Money|\Money\Money $first, Money|\Money\Money ...$collection)
* @method static Money sum(Money|\Money\Money $first, Money|\Money\Money ...$collection)
* @method static Money avg(Money|\Money\Money $first, Money|\Money\Money ...$collection)
*/
class Money implements Arrayable, Jsonable, JsonSerializable
{
use CurrenciesTrait;
use LocaleTrait;
use Macroable {
Macroable::__call as macroCall;
Macroable::__callStatic as macroCallStatic;
}
use MoneyFactory {
MoneyFactory::__callStatic as factoryCallStatic;
}
use MoneyFormatterTrait;
use MoneyParserTrait;
use MoneySerializerTrait;
/**
* @var \Money\Money
*/
protected $money;
/**
* @var array
*/
protected $attributes = [];
/**
* Money.
*
* @param mixed $amount
* @param \Money\Currency|string|null $currency
* @param bool $forceDecimals
* @param string|null $locale
* @param \Money\Currencies|null $currencies
*/
public function __construct($amount = null, $currency = null, $forceDecimals = false, $locale = null, $currencies = null)
{
$this->money = Money::parse($amount, $currency, $forceDecimals, $locale, $currencies, null, false);
}
/**
* Get money.
*
* @return \Money\Money
*/
public function getMoney()
{
return $this->money;
}
/**
* Get attributes.
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* Divide.
*
* @param int|string|float $divisor
* @param int $roundingMode
* @return \Cknow\Money\Money
*/
public function divide($divisor, $roundingMode = \Money\Money::ROUND_HALF_UP)
{
return $this->__call('divide', [
is_int($divisor) ? $divisor : strval($divisor),
$roundingMode,
]);
}
/**
* Multiply.
*
* @param int|string|float $multiplier
* @param int $roundingMode
* @return \Cknow\Money\Money
*/
public function multiply($multiplier, $roundingMode = \Money\Money::ROUND_HALF_UP)
{
return $this->__call('multiply', [
is_int($multiplier) ? $multiplier : strval($multiplier),
$roundingMode,
]);
}
/**
* Attributes.
*/
public function attributes(array $attributes = [])
{
$this->attributes = $attributes;
}
/**
* Json serialize.
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->serialize();
}
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->jsonSerialize();
}
/**
* Convert the object to its JSON representation.
*
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->toArray(), $options);
}
/**
* __toString.
*
* @return string
*/
public function __toString()
{
return $this->format();
}
/**
* __call.
*
* @param string $method
* @return \Cknow\Money\Money|\Cknow\Money\Money[]|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, array $arguments)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $arguments);
}
if (! method_exists($this->money, $method)) {
throw new BadMethodCallException(sprintf(
'Call to undefined method %s::%s()',
static::class,
$method
));
}
$result = call_user_func_array([$this->money, $method], static::getArguments($arguments));
$methods = [
'add', 'subtract',
'multiply', 'divide', 'mod',
'allocate', 'allocateTo',
'absolute', 'negative',
];
if (! in_array($method, $methods)) {
return $result;
}
return static::convertResult($result);
}
/**
* Convert.
*
* @return \Cknow\Money\Money
*/
public static function convert(\Money\Money $instance)
{
return static::fromMoney($instance);
}
/**
* __callStatic.
*
* @param string $method
* @return \Cknow\Money\Money
*/
public static function __callStatic($method, array $arguments)
{
if (static::hasMacro($method)) {
return static::macroCallStatic($method, $arguments);
}
if (in_array($method, ['min', 'max', 'avg', 'sum'])) {
$result = call_user_func_array([\Money\Money::class, $method], static::getArguments($arguments));
return static::convert($result);
}
return static::factoryCallStatic($method, $arguments);
}
/**
* Get arguments.
*
* @return array
*/
private static function getArguments(array $arguments = [])
{
$args = [];
foreach ($arguments as $argument) {
$args[] = $argument instanceof static ? $argument->getMoney() : $argument;
}
return $args;
}
/**
* Convert result.
*
* @param mixed $result
* @return \Cknow\Money\Money|\Cknow\Money\Money[]
*/
private static function convertResult($result)
{
if (! is_array($result)) {
return static::convert($result);
}
$results = [];
foreach ($result as $item) {
$results[] = static::convert($item);
}
return $results;
}
}