tutor
/
ecommerce
/
PaymentGateways
/
Paypal
/
vendor
/
brick
/
money
/
src
/
ISOCurrencyProvider.php
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 |