1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Money\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\Money\MoneyServiceProvider; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Money\Currencies; |
8
|
|
|
use Money\Currency; |
9
|
|
|
use Money\Currencies\ISOCurrencies; |
10
|
|
|
use Money\Currencies\BitcoinCurrencies; |
11
|
|
|
use Money\Currencies\AggregateCurrencies; |
12
|
|
|
use Money\Currencies\CurrencyList; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait CurrenciesTrait |
16
|
|
|
* @package ByTIC\Money\Traits |
17
|
|
|
* |
18
|
|
|
* @method Currency getCurrency() |
19
|
|
|
*/ |
20
|
|
|
trait CurrenciesTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected static $currency; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Money\Currencies |
29
|
|
|
*/ |
30
|
|
|
protected static $currencies; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param $code |
34
|
|
|
* @return Currency |
35
|
|
|
*/ |
36
|
|
|
public static function currency($code = null) |
37
|
|
|
{ |
38
|
|
|
if ($code instanceof Currency) { |
39
|
|
|
return $code; |
40
|
|
|
} |
41
|
|
|
$code = $code ?: static::currencyDefault(); |
42
|
|
|
return new Currency($code); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public static function currencyDefault() |
49
|
|
|
{ |
50
|
|
|
return 'USD'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get currencies. |
55
|
|
|
* |
56
|
|
|
* @return \Money\Currencies |
57
|
|
|
*/ |
58
|
|
|
public static function getCurrencies() |
59
|
|
|
{ |
60
|
|
|
if (!isset(static::$currencies)) { |
61
|
|
|
$container = \Nip\Container\Utility\Container::container(); |
62
|
|
|
static::setCurrencies($container->has(MoneyServiceProvider::MONEY_CURRENCIES) ? $container->get(MoneyServiceProvider::MONEY_CURRENCIES) : []); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return static::$currencies; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set currencies. |
70
|
|
|
* |
71
|
|
|
* @param \Money\Currencies|array|null $currencies |
72
|
|
|
*/ |
73
|
|
|
public static function setCurrencies($currencies) |
74
|
|
|
{ |
75
|
|
|
static::$currencies = ($currencies instanceof Currencies) |
76
|
|
|
? $currencies |
77
|
|
|
: static::makeCurrencies($currencies); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Make currencies according to array derived from config or anywhere else. |
82
|
|
|
* |
83
|
|
|
* @param array|null $currenciesConfig |
84
|
|
|
* |
85
|
|
|
* @return \Money\Currencies |
86
|
|
|
*/ |
87
|
|
|
private static function makeCurrencies($currenciesConfig) |
88
|
|
|
{ |
89
|
|
|
if (!$currenciesConfig || !is_array($currenciesConfig)) { |
90
|
|
|
// for backward compatibility |
91
|
|
|
return new ISOCurrencies(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$currenciesList = []; |
95
|
|
|
|
96
|
|
|
if ($currenciesConfig['iso'] ?? false) { |
97
|
|
|
$currenciesList[] = static::makeCurrenciesForSource( |
98
|
|
|
$currenciesConfig['iso'], |
99
|
|
|
new ISOCurrencies(), |
100
|
|
|
'ISO' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($currenciesConfig['bitcoin'] ?? false) { |
105
|
|
|
$currenciesList[] = static::makeCurrenciesForSource( |
106
|
|
|
$currenciesConfig['bitcoin'], |
107
|
|
|
new BitcoinCurrencies(), |
108
|
|
|
'Bitcoin' |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($currenciesConfig['custom'] ?? false) { |
113
|
|
|
$currenciesList[] = new CurrencyList($currenciesConfig['custom']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return new AggregateCurrencies($currenciesList); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Make currencies list according to array for specified source. |
121
|
|
|
* |
122
|
|
|
* @param array|string $config |
123
|
|
|
* @param \Money\Currencies $currencies |
124
|
|
|
* @param string $sourceName |
125
|
|
|
* |
126
|
|
|
* @throws \InvalidArgumentException |
127
|
|
|
* |
128
|
|
|
* @return \Money\Currencies |
129
|
|
|
*/ |
130
|
|
|
private static function makeCurrenciesForSource($config, Currencies $currencies, $sourceName) |
131
|
|
|
{ |
132
|
|
|
if ($config === 'all') { |
133
|
|
|
return $currencies; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (is_array($config)) { |
137
|
|
|
$lisCurrencies = []; |
138
|
|
|
|
139
|
|
|
foreach ($config as $index => $currencyCode) { |
140
|
|
|
$currency = new Currency($currencyCode); |
141
|
|
|
|
142
|
|
|
if (!$currencies->contains($currency)) { |
143
|
|
|
throw new InvalidArgumentException( |
144
|
|
|
sprintf('Unknown %s currency code: %s', $sourceName, $currencyCode) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$lisCurrencies[$currency->getCode()] = $currencies->subunitFor($currency); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return new CurrencyList($lisCurrencies); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
throw new InvalidArgumentException( |
155
|
|
|
sprintf('%s config must be an array or \'all\'', $sourceName) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |