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