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.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / PaymentGateways / Paypal / vendor / brick / money / src / ISOCurrencyProvider.php
tutor / ecommerce / PaymentGateways / Paypal / vendor / brick / money / src Last commit date
Context 1 year ago Exception 1 year ago ExchangeRateProvider 1 year ago AbstractMoney.php 1 year ago Context.php 1 year ago Currency.php 1 year ago CurrencyConverter.php 1 year ago ExchangeRateProvider.php 1 year ago ISOCurrencyProvider.php 1 year ago Money.php 1 year ago MoneyBag.php 1 year ago MoneyComparator.php 1 year ago MoneyContainer.php 1 year ago RationalMoney.php 1 year ago
ISOCurrencyProvider.php
199 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Brick\Money;
6
7 use Brick\Money\Exception\UnknownCurrencyException;
8
9 /**
10 * Provides ISO 4217 currencies.
11 */
12 final class ISOCurrencyProvider
13 {
14 private static ?ISOCurrencyProvider $instance = null;
15
16 /**
17 * The raw currency data, indexed by currency code.
18 *
19 * @psalm-var array<string, array{string, int, string, int}>
20 */
21 private array $currencyData;
22
23 /**
24 * An associative array of currency numeric code to currency code.
25 *
26 * This property is set on-demand, as soon as required.
27 *
28 * @psalm-var array<int, string>|null
29 */
30 private ?array $numericToCurrency = null;
31
32 /**
33 * An associative array of country code to currency codes.
34 *
35 * This property is set on-demand, as soon as required.
36 *
37 * @psalm-var array<string, list<string>>|null
38 */
39 private ?array $countryToCurrency = null;
40
41 /**
42 * The Currency instances.
43 *
44 * The instances are created on-demand, as soon as they are requested.
45 *
46 * @psalm-var array<string, Currency>
47 *
48 * @var Currency[]
49 */
50 private array $currencies = [];
51
52 /**
53 * Whether the provider is in a partial state.
54 *
55 * This is true as long as all the currencies have not been instantiated yet.
56 */
57 private bool $isPartial = true;
58
59 /**
60 * Private constructor. Use `getInstance()` to obtain the singleton instance.
61 */
62 private function __construct()
63 {
64 $this->currencyData = require __DIR__ . '/../data/iso-currencies.php';
65 }
66
67 /**
68 * Returns the singleton instance of ISOCurrencyProvider.
69 *
70 * @return ISOCurrencyProvider
71 */
72 public static function getInstance() : ISOCurrencyProvider
73 {
74 if (self::$instance === null) {
75 self::$instance = new ISOCurrencyProvider();
76 }
77
78 return self::$instance;
79 }
80
81 /**
82 * Returns the currency matching the given currency code.
83 *
84 * @param string|int $currencyCode The 3-letter or numeric ISO 4217 currency code.
85 *
86 * @return Currency The currency.
87 *
88 * @throws UnknownCurrencyException If the currency code is not known.
89 */
90 public function getCurrency($currencyCode) : Currency
91 {
92 if (is_int($currencyCode)) {
93 if ($this->numericToCurrency === null) {
94 $this->numericToCurrency = require __DIR__ . '/../data/numeric-to-currency.php';
95 }
96
97 if (isset($this->numericToCurrency[$currencyCode])) {
98 return $this->getCurrency($this->numericToCurrency[$currencyCode]);
99 }
100
101 throw UnknownCurrencyException::unknownCurrency($currencyCode);
102 }
103
104 if (isset($this->currencies[$currencyCode])) {
105 return $this->currencies[$currencyCode];
106 }
107
108 if (! isset($this->currencyData[$currencyCode])) {
109 throw UnknownCurrencyException::unknownCurrency($currencyCode);
110 }
111
112 $currency = new Currency(... $this->currencyData[$currencyCode]);
113
114 return $this->currencies[$currencyCode] = $currency;
115 }
116
117 /**
118 * Returns all the available currencies.
119 *
120 * @psalm-return array<string, Currency>
121 *
122 * @return Currency[] The currencies, indexed by currency code.
123 */
124 public function getAvailableCurrencies() : array
125 {
126 if ($this->isPartial) {
127 foreach ($this->currencyData as $currencyCode => $data) {
128 if (! isset($this->currencies[$currencyCode])) {
129 $this->currencies[$currencyCode] = new Currency(... $data);
130 }
131 }
132
133 ksort($this->currencies);
134
135 $this->isPartial = false;
136 }
137
138 return $this->currencies;
139 }
140
141 /**
142 * Returns the currency for the given ISO country code.
143 *
144 * @param string $countryCode The 2-letter ISO 3166-1 country code.
145 *
146 * @return Currency
147 *
148 * @throws UnknownCurrencyException If the country code is not known, or the country has no single currency.
149 */
150 public function getCurrencyForCountry(string $countryCode) : Currency
151 {
152 $currencies = $this->getCurrenciesForCountry($countryCode);
153
154 $count = count($currencies);
155
156 if ($count === 1) {
157 return $currencies[0];
158 }
159
160 if ($count === 0) {
161 throw UnknownCurrencyException::noCurrencyForCountry($countryCode);
162 }
163
164 $currencyCodes = [];
165
166 foreach ($currencies as $currency) {
167 $currencyCodes[] = $currency->getCurrencyCode();
168 }
169
170 throw UnknownCurrencyException::noSingleCurrencyForCountry($countryCode, $currencyCodes);
171 }
172
173 /**
174 * Returns the currencies for the given ISO country code.
175 *
176 * If the country code is not known, or if the country has no official currency, an empty array is returned.
177 *
178 * @param string $countryCode The 2-letter ISO 3166-1 country code.
179 *
180 * @return Currency[]
181 */
182 public function getCurrenciesForCountry(string $countryCode) : array
183 {
184 if ($this->countryToCurrency === null) {
185 $this->countryToCurrency = require __DIR__ . '/../data/country-to-currency.php';
186 }
187
188 $result = [];
189
190 if (isset($this->countryToCurrency[$countryCode])) {
191 foreach ($this->countryToCurrency[$countryCode] as $currencyCode) {
192 $result[] = $this->getCurrency($currencyCode);
193 }
194 }
195
196 return $result;
197 }
198 }
199