forked from akaunting/laravel-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Currency.php
368 lines (327 loc) · 10.1 KB
/
Currency.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
namespace Akaunting\Money;
use Akaunting\Money\Casts\CurrencyCast;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
use OutOfBoundsException;
/**
* Class Currency.
*
* @method static Currency AED()
* @method static Currency AFN()
* @method static Currency ALL()
* @method static Currency AMD()
* @method static Currency ANG()
* @method static Currency AOA()
* @method static Currency ARS()
* @method static Currency AUD()
* @method static Currency AWG()
* @method static Currency AZN()
* @method static Currency BAM()
* @method static Currency BBD()
* @method static Currency BDT()
* @method static Currency BGN()
* @method static Currency BHD()
* @method static Currency BIF()
* @method static Currency BMD()
* @method static Currency BND()
* @method static Currency BOB()
* @method static Currency BOV()
* @method static Currency BRL()
* @method static Currency BSD()
* @method static Currency BTN()
* @method static Currency BWP()
* @method static Currency BYR()
* @method static Currency BZD()
* @method static Currency CAD()
* @method static Currency CDF()
* @method static Currency CHF()
* @method static Currency CLF()
* @method static Currency CLP()
* @method static Currency CNY()
* @method static Currency COP()
* @method static Currency CRC()
* @method static Currency CUC()
* @method static Currency CUP()
* @method static Currency CVE()
* @method static Currency CZK()
* @method static Currency DJF()
* @method static Currency DKK()
* @method static Currency DOP()
* @method static Currency DZD()
* @method static Currency EGP()
* @method static Currency ERN()
* @method static Currency ETB()
* @method static Currency EUR()
* @method static Currency FJD()
* @method static Currency FKP()
* @method static Currency GBP()
* @method static Currency GEL()
* @method static Currency GHS()
* @method static Currency GIP()
* @method static Currency GMD()
* @method static Currency GNF()
* @method static Currency GTQ()
* @method static Currency GYD()
* @method static Currency HKD()
* @method static Currency HNL()
* @method static Currency HRK()
* @method static Currency HTG()
* @method static Currency HUF()
* @method static Currency IDR()
* @method static Currency ILS()
* @method static Currency INR()
* @method static Currency IQD()
* @method static Currency IRR()
* @method static Currency ISK()
* @method static Currency JMD()
* @method static Currency JOD()
* @method static Currency JPY()
* @method static Currency KES()
* @method static Currency KGS()
* @method static Currency KHR()
* @method static Currency KMF()
* @method static Currency KPW()
* @method static Currency KRW()
* @method static Currency KWD()
* @method static Currency KYD()
* @method static Currency KZT()
* @method static Currency LAK()
* @method static Currency LBP()
* @method static Currency LKR()
* @method static Currency LRD()
* @method static Currency LSL()
* @method static Currency LTL()
* @method static Currency LVL()
* @method static Currency LYD()
* @method static Currency MAD()
* @method static Currency MDL()
* @method static Currency MGA()
* @method static Currency MKD()
* @method static Currency MMK()
* @method static Currency MNT()
* @method static Currency MOP()
* @method static Currency MRO()
* @method static Currency MUR()
* @method static Currency MVR()
* @method static Currency MWK()
* @method static Currency MXN()
* @method static Currency MYR()
* @method static Currency MZN()
* @method static Currency NAD()
* @method static Currency NGN()
* @method static Currency NIO()
* @method static Currency NOK()
* @method static Currency NPR()
* @method static Currency NZD()
* @method static Currency OMR()
* @method static Currency PAB()
* @method static Currency PEN()
* @method static Currency PGK()
* @method static Currency PHP()
* @method static Currency PKR()
* @method static Currency PLN()
* @method static Currency PYG()
* @method static Currency QAR()
* @method static Currency RON()
* @method static Currency RSD()
* @method static Currency RUB()
* @method static Currency RWF()
* @method static Currency SAR()
* @method static Currency SBD()
* @method static Currency SCR()
* @method static Currency SDG()
* @method static Currency SEK()
* @method static Currency SGD()
* @method static Currency SHP()
* @method static Currency SLL()
* @method static Currency SOS()
* @method static Currency SRD()
* @method static Currency SSP()
* @method static Currency STD()
* @method static Currency SVC()
* @method static Currency SYP()
* @method static Currency SZL()
* @method static Currency THB()
* @method static Currency TJS()
* @method static Currency TMT()
* @method static Currency TND()
* @method static Currency TOP()
* @method static Currency TRY()
* @method static Currency TTD()
* @method static Currency TWD()
* @method static Currency TZS()
* @method static Currency UAH()
* @method static Currency UGX()
* @method static Currency USD()
* @method static Currency UYU()
* @method static Currency UZS()
* @method static Currency VEF()
* @method static Currency VND()
* @method static Currency VUV()
* @method static Currency WST()
* @method static Currency XAF()
* @method static Currency XAG()
* @method static Currency XAU()
* @method static Currency XCD()
* @method static Currency XDR()
* @method static Currency XOF()
* @method static Currency XPF()
* @method static Currency YER()
* @method static Currency ZAR()
* @method static Currency ZMW()
* @method static Currency ZWL()
*/
class Currency implements Arrayable, Castable, Jsonable, JsonSerializable, Renderable
{
use Macroable;
protected string $currency;
protected string $name;
protected int $code;
protected float $rate;
protected int $precision;
protected int $subunit;
protected string $symbol;
protected bool $symbolFirst;
protected string $decimalMark;
protected string $thousandsSeparator;
protected static array $currencies;
/**
* Create a new instance.
*
* @throws OutOfBoundsException
*/
public function __construct(string $currency)
{
$currency = strtoupper(trim($currency));
$currencies = static::getCurrencies();
if (!array_key_exists($currency, $currencies)) {
throw new OutOfBoundsException('Invalid currency "' . $currency . '"');
}
$attributes = (array) $currencies[$currency];
$this->currency = $currency;
$this->name = (string) $attributes['name'];
$this->code = (int) $attributes['code'];
$this->rate = (float) (isset($attributes['rate']) ? $attributes['rate'] : 1);
$this->precision = (int) $attributes['precision'];
$this->subunit = (int) $attributes['subunit'];
$this->symbol = (string) $attributes['symbol'];
$this->symbolFirst = (bool) $attributes['symbol_first'];
$this->decimalMark = (string) $attributes['decimal_mark'];
$this->thousandsSeparator = (string) $attributes['thousands_separator'];
}
public static function __callStatic(string $method, array $arguments): Currency
{
return new self($method);
}
/**
* @return class-string<CastsAttributes>
*/
public static function castUsing(array $arguments): string
{
return CurrencyCast::class;
}
public static function setCurrencies(array $currencies): void
{
static::$currencies = $currencies;
}
public static function getCurrencies(): array
{
return static::$currencies ??= require __DIR__ . '/../config/money.php';
}
public function equals(Currency $currency): bool
{
return $this->getCurrency() === $currency->getCurrency();
}
public function getCurrency(): string
{
return $this->currency;
}
public function getName(): string
{
return $this->name;
}
public function getCode(): int
{
return $this->code;
}
public function getRate(): float
{
return $this->rate;
}
public function getPrecision(): int
{
return $this->precision;
}
public function getSubunit(): int
{
return $this->subunit;
}
public function getSymbol(): string
{
return $this->symbol;
}
public function isSymbolFirst(): bool
{
return $this->symbolFirst;
}
public function getDecimalMark(): string
{
return $this->decimalMark;
}
public function getThousandsSeparator(): string
{
return $this->thousandsSeparator;
}
public function getPrefix(): string
{
if (!$this->symbolFirst) {
return '';
}
return $this->symbol;
}
public function getSuffix(): string
{
if ($this->symbolFirst) {
return '';
}
return ' ' . $this->symbol;
}
public function toArray(): array
{
return [$this->currency => [
'name' => $this->name,
'code' => $this->code,
'rate' => $this->rate,
'precision' => $this->precision,
'subunit' => $this->subunit,
'symbol' => $this->symbol,
'symbol_first' => $this->symbolFirst,
'decimal_mark' => $this->decimalMark,
'thousands_separator' => $this->thousandsSeparator,
'prefix' => $this->getPrefix(),
'suffix' => $this->getSuffix(),
]];
}
public function toJson($options = 0): string
{
return json_encode($this->toArray(), $options);
}
public function jsonSerialize(): array
{
return $this->toArray();
}
public function render(): string
{
return $this->currency . ' (' . $this->name . ')';
}
public function __toString(): string
{
return $this->render();
}
}