diff options
Diffstat (limited to 'vendor/commerceguys/intl/src/Currency')
5 files changed, 318 insertions, 238 deletions
diff --git a/vendor/commerceguys/intl/src/Currency/Currency.php b/vendor/commerceguys/intl/src/Currency/Currency.php index 6f9256bf0..28329795c 100644 --- a/vendor/commerceguys/intl/src/Currency/Currency.php +++ b/vendor/commerceguys/intl/src/Currency/Currency.php @@ -2,7 +2,10 @@ namespace CommerceGuys\Intl\Currency; -class Currency implements CurrencyEntityInterface +/** + * Represents a currency. + */ +final class Currency { /** * The alphanumeric currency code. @@ -37,47 +40,67 @@ class Currency implements CurrencyEntityInterface * * @var int */ - protected $fractionDigits; + protected $fractionDigits = 2; /** - * The currency locale (i.e. "en_US"). - * - * The currency name and symbol are locale specific. + * The locale (i.e. "en_US"). * * @var string */ protected $locale; /** - * Returns the string representation of the currency. + * Creates a new Currency instance. * - * @return string + * @param array $definition The definition array. */ - public function __toString() + public function __construct(array $definition) { - return $this->getCurrencyCode(); + foreach (['currency_code', 'name', 'numeric_code', 'locale'] as $requiredProperty) { + if (empty($definition[$requiredProperty])) { + throw new \InvalidArgumentException(sprintf('Missing required property "%s".', $requiredProperty)); + } + } + if (!isset($definition['symbol'])) { + $definition['symbol'] = $definition['currency_code']; + } + + $this->currencyCode = $definition['currency_code']; + $this->name = $definition['name']; + $this->numericCode = $definition['numeric_code']; + $this->symbol = $definition['symbol']; + if (isset($definition['fraction_digits'])) { + $this->fractionDigits = $definition['fraction_digits']; + } + $this->locale = $definition['locale']; } /** - * {@inheritdoc} + * Gets the string representation of the currency. + * + * @return string */ - public function getCurrencyCode() + public function __toString() { return $this->currencyCode; } /** - * {@inheritdoc} + * Gets the alphabetic currency code. + * + * @return string */ - public function setCurrencyCode($currencyCode) + public function getCurrencyCode() { - $this->currencyCode = $currencyCode; - - return $this; + return $this->currencyCode; } /** - * {@inheritdoc} + * Gets the currency name. + * + * This value is locale specific. + * + * @return string */ public function getName() { @@ -85,17 +108,12 @@ class Currency implements CurrencyEntityInterface } /** - * {@inheritdoc} - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * {@inheritdoc} + * Gets the numeric currency code. + * + * The numeric code has three digits, and the first one can be a zero, + * hence the need to pass it around as a string. + * + * @return string */ public function getNumericCode() { @@ -103,17 +121,11 @@ class Currency implements CurrencyEntityInterface } /** - * {@inheritdoc} - */ - public function setNumericCode($numericCode) - { - $this->numericCode = $numericCode; - - return $this; - } - - /** - * {@inheritdoc} + * Gets the currency symbol. + * + * This value is locale specific. + * + * @return string */ public function getSymbol() { @@ -121,17 +133,12 @@ class Currency implements CurrencyEntityInterface } /** - * {@inheritdoc} - */ - public function setSymbol($symbol) - { - $this->symbol = $symbol; - - return $this; - } - - /** - * {@inheritdoc} + * Gets the number of fraction digits. + * + * Used when rounding or formatting an amount for display. + * Actual storage precision can be greater. + * + * @return int */ public function getFractionDigits() { @@ -139,30 +146,14 @@ class Currency implements CurrencyEntityInterface } /** - * {@inheritdoc} - */ - public function setFractionDigits($fractionDigits) - { - $this->fractionDigits = $fractionDigits; - - return $this; - } - - /** - * {@inheritdoc} + * Gets the locale. + * + * The currency name and symbol are locale specific. + * + * @return string */ public function getLocale() { return $this->locale; } - - /** - * {@inheritdoc} - */ - public function setLocale($locale) - { - $this->locale = $locale; - - return $this; - } } diff --git a/vendor/commerceguys/intl/src/Currency/CurrencyEntityInterface.php b/vendor/commerceguys/intl/src/Currency/CurrencyEntityInterface.php deleted file mode 100644 index 85d4ab83d..000000000 --- a/vendor/commerceguys/intl/src/Currency/CurrencyEntityInterface.php +++ /dev/null @@ -1,51 +0,0 @@ -<?php - -namespace CommerceGuys\Intl\Currency; - -interface CurrencyEntityInterface extends CurrencyInterface -{ - /** - * Sets the alphabetic currency code. - * - * @param string $currencyCode The alphabetic currency code. - * - * @return self - */ - public function setCurrencyCode($currencyCode); - - /** - * Sets the currency name. - * - * @param string $name The currency name. - * - * @return self - */ - public function setName($name); - - /** - * Sets the numeric currency code. - * - * @param string $numericCode The numeric currency code. - * - * @return self - */ - public function setNumericCode($numericCode); - - /** - * Sets the currency symbol. - * - * @param string $symbol The currency symbol. - * - * @return self - */ - public function setSymbol($symbol); - - /** - * Sets the number of fraction digits. - * - * @param int $fractionDigits The number of fraction digits. - * - * @return self - */ - public function setFractionDigits($fractionDigits); -} diff --git a/vendor/commerceguys/intl/src/Currency/CurrencyInterface.php b/vendor/commerceguys/intl/src/Currency/CurrencyInterface.php deleted file mode 100644 index 8aa709ca0..000000000 --- a/vendor/commerceguys/intl/src/Currency/CurrencyInterface.php +++ /dev/null @@ -1,47 +0,0 @@ -<?php - -namespace CommerceGuys\Intl\Currency; - -interface CurrencyInterface -{ - /** - * Gets the alphabetic currency code. - * - * @return string - */ - public function getCurrencyCode(); - - /** - * Gets the currency name. - * - * @return string - */ - public function getName(); - - /** - * Gets the numeric currency code. - * - * The numeric code has three digits, and the first one can be a zero, - * hence the need to pass it around as a string. - * - * @return string - */ - public function getNumericCode(); - - /** - * Gets the currency symbol. - * - * @return string - */ - public function getSymbol(); - - /** - * Gets the number of fraction digits. - * - * Used when rounding or formatting an amount for display. - * Actual storage precision can be greater. - * - * @return int - */ - public function getFractionDigits(); -} diff --git a/vendor/commerceguys/intl/src/Currency/CurrencyRepository.php b/vendor/commerceguys/intl/src/Currency/CurrencyRepository.php index 5fdfce216..5a8dc2163 100644 --- a/vendor/commerceguys/intl/src/Currency/CurrencyRepository.php +++ b/vendor/commerceguys/intl/src/Currency/CurrencyRepository.php @@ -2,7 +2,7 @@ namespace CommerceGuys\Intl\Currency; -use CommerceGuys\Intl\LocaleResolverTrait; +use CommerceGuys\Intl\Locale; use CommerceGuys\Intl\Exception\UnknownCurrencyException; /** @@ -10,17 +10,26 @@ use CommerceGuys\Intl\Exception\UnknownCurrencyException; */ class CurrencyRepository implements CurrencyRepositoryInterface { - use LocaleResolverTrait; + /** + * The default locale. + * + * @var string + */ + protected $defaultLocale; /** - * Base currency definitions. + * The fallback locale. * - * Contains data common to all locales, such as the currency numeric - * code, number of fraction digits. + * @var string + */ + protected $fallbackLocale; + + /** + * The path where per-locale definitions are stored. * - * @var array + * @var string */ - protected $baseDefinitions = []; + protected $definitionPath; /** * Per-locale currency definitions. @@ -30,40 +39,87 @@ class CurrencyRepository implements CurrencyRepositoryInterface protected $definitions = []; /** + * The available locales. + * + * @var array + */ + protected $availableLocales = [ + 'af', 'agq', 'ak', 'am', 'ar', 'as', 'asa', 'ast', 'az', 'bas', 'be', + 'bez', 'bg', 'bm', 'bn', 'br', 'brx', 'bs', 'bs-Cyrl', 'ca', 'ccp', + 'ce', 'ceb', 'cgg', 'chr', 'cs', 'cy', 'da', 'dav', 'de', 'de-CH', + 'dje', 'dsb', 'dz', 'ebu', 'ee', 'el', 'en', 'en-001', 'en-AU', 'en-GG', + 'en-IM', 'en-JE', 'es', 'es-419', 'es-CL', 'es-GT', 'es-MX', 'es-US', + 'es-VE', 'et', 'eu', 'ewo', 'fa', 'fa-AF', 'ff', 'fi', 'fil', 'fo', + 'fr', 'fr-CA', 'fur', 'fy', 'ga', 'gd', 'gl', 'gsw', 'gu', 'guz', 'ha', + 'he', 'hi', 'hr', 'hsb', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'jmc', + 'jv', 'ka', 'kab', 'kam', 'kde', 'kea', 'khq', 'ki', 'kk', 'kln', 'km', + 'kn', 'ko', 'kok', 'ks', 'ksb', 'ksf', 'ksh', 'ky', 'lag', 'lb', 'lg', + 'ln', 'lo', 'lt', 'lu', 'luo', 'luy', 'lv', 'mas', 'mer', 'mfe', 'mg', + 'mk', 'ml', 'mn', 'mr', 'ms', 'mua', 'my', 'mzn', 'naq', 'nb', 'nd', + 'ne', 'nl', 'nmg', 'nn', 'nyn', 'or', 'pa', 'pl', 'ps', 'pt', 'pt-PT', + 'qu', 'rm', 'rn', 'ro', 'rof', 'ru', 'rwk', 'saq', 'sbp', 'sd', 'seh', + 'ses', 'sg', 'shi', 'shi-Latn', 'si', 'sk', 'sl', 'sn', 'so', 'sq', + 'sr', 'sr-Latn', 'sv', 'sw', 'sw-CD', 'sw-KE', 'ta', 'te', 'teo', 'th', + 'tk', 'tr', 'twq', 'tzm', 'ug', 'uk', 'ur', 'ur-IN', 'uz', 'uz-Cyrl', + 'vai', 'vai-Latn', 'vi', 'vun', 'xog', 'yo', 'yo-BJ', 'yue', 'yue-Hans', + 'zgh', 'zh', 'zh-Hans-HK', 'zh-Hant', 'zh-Hant-HK', 'zu', + ]; + + /** * Creates a CurrencyRepository instance. * + * @param string $defaultLocale The default locale. Defaults to 'en'. + * @param string $fallbackLocale The fallback locale. Defaults to 'en'. * @param string $definitionPath The path to the currency definitions. * Defaults to 'resources/currency'. */ - public function __construct($definitionPath = null) + public function __construct($defaultLocale = 'en', $fallbackLocale = 'en', $definitionPath = null) { + $this->defaultLocale = $defaultLocale; + $this->fallbackLocale = $fallbackLocale; $this->definitionPath = $definitionPath ? $definitionPath : __DIR__ . '/../../resources/currency/'; } /** * {@inheritdoc} */ - public function get($currencyCode, $locale = null, $fallbackLocale = null) + public function get($currencyCode, $locale = null) { - $locale = $this->resolveLocale($locale, $fallbackLocale); - $definitions = $this->loadDefinitions($locale); - if (!isset($definitions[$currencyCode])) { + $currencyCode = strtoupper($currencyCode); + $baseDefinitions = $this->getBaseDefinitions(); + if (!isset($baseDefinitions[$currencyCode])) { throw new UnknownCurrencyException($currencyCode); } + $locale = $locale ?: $this->defaultLocale; + $locale = Locale::resolve($this->availableLocales, $locale, $this->fallbackLocale); + $definitions = $this->loadDefinitions($locale); + $currency = new Currency([ + 'currency_code' => $currencyCode, + 'numeric_code' => $baseDefinitions[$currencyCode][0], + 'fraction_digits' => $baseDefinitions[$currencyCode][1], + 'locale' => $locale, + ] + $definitions[$currencyCode]); - return $this->createCurrencyFromDefinition($currencyCode, $definitions[$currencyCode], $locale); + return $currency; } /** * {@inheritdoc} */ - public function getAll($locale = null, $fallbackLocale = null) + public function getAll($locale = null) { - $locale = $this->resolveLocale($locale, $fallbackLocale); + $locale = $locale ?: $this->defaultLocale; + $locale = Locale::resolve($this->availableLocales, $locale, $this->fallbackLocale); + $baseDefinitions = $this->getBaseDefinitions(); $definitions = $this->loadDefinitions($locale); $currencies = []; foreach ($definitions as $currencyCode => $definition) { - $currencies[$currencyCode] = $this->createCurrencyFromDefinition($currencyCode, $definition, $locale); + $currencies[$currencyCode] = new Currency([ + 'currency_code' => $currencyCode, + 'numeric_code' => $baseDefinitions[$currencyCode][0], + 'fraction_digits' => $baseDefinitions[$currencyCode][1], + 'locale' => $locale, + ] + $definition); } return $currencies; @@ -72,9 +128,10 @@ class CurrencyRepository implements CurrencyRepositoryInterface /** * {@inheritdoc} */ - public function getList($locale = null, $fallbackLocale = null) + public function getList($locale = null) { - $locale = $this->resolveLocale($locale, $fallbackLocale); + $locale = $locale ?: $this->defaultLocale; + $locale = Locale::resolve($this->availableLocales, $locale, $this->fallbackLocale); $definitions = $this->loadDefinitions($locale); $list = []; foreach ($definitions as $currencyCode => $definition) { @@ -96,49 +153,183 @@ class CurrencyRepository implements CurrencyRepositoryInterface if (!isset($this->definitions[$locale])) { $filename = $this->definitionPath . $locale . '.json'; $this->definitions[$locale] = json_decode(file_get_contents($filename), true); - - // Make sure the base definitions have been loaded. - if (empty($this->baseDefinitions)) { - $this->baseDefinitions = json_decode(file_get_contents($this->definitionPath . 'base.json'), true); - } - // Merge-in base definitions. - foreach ($this->definitions[$locale] as $currencyCode => $definition) { - $this->definitions[$locale][$currencyCode] += $this->baseDefinitions[$currencyCode]; - } } return $this->definitions[$locale]; } /** - * Creates a currency object from the provided definition. + * Gets the base currency definitions. * - * @param string $currencyCode The currency code. - * @param array $definition The currency definition. - * @param string $locale The locale of the currency definition. + * Contains data common to all locales: numeric code, fraction digits. * - * @return Currency + * @return array + * An array of definitions, keyed by currency code. + * Each definition is a numerically indexed array containing: + * - The numeric code. + * - The fraction digits. */ - protected function createCurrencyFromDefinition($currencyCode, array $definition, $locale) + protected function getBaseDefinitions() { - if (!isset($definition['symbol'])) { - $definition['symbol'] = $currencyCode; - } - if (!isset($definition['fraction_digits'])) { - $definition['fraction_digits'] = 2; - } - - $currency = new Currency(); - $setValues = \Closure::bind(function ($currencyCode, $definition, $locale) { - $this->currencyCode = $currencyCode; - $this->name = $definition['name']; - $this->numericCode = $definition['numeric_code']; - $this->symbol = $definition['symbol']; - $this->fractionDigits = $definition['fraction_digits']; - $this->locale = $locale; - }, $currency, '\CommerceGuys\Intl\Currency\Currency'); - $setValues($currencyCode, $definition, $locale); - - return $currency; + return [ + 'AED' => ['784', 2], + 'AFN' => ['971', 0], + 'ALL' => ['008', 0], + 'AMD' => ['051', 2], + 'ANG' => ['532', 2], + 'AOA' => ['973', 2], + 'ARS' => ['032', 2], + 'AUD' => ['036', 2], + 'AWG' => ['533', 2], + 'AZN' => ['944', 2], + 'BAM' => ['977', 2], + 'BBD' => ['052', 2], + 'BDT' => ['050', 2], + 'BGN' => ['975', 2], + 'BHD' => ['048', 3], + 'BIF' => ['108', 0], + 'BMD' => ['060', 2], + 'BND' => ['096', 2], + 'BOB' => ['068', 2], + 'BRL' => ['986', 2], + 'BSD' => ['044', 2], + 'BTN' => ['064', 2], + 'BWP' => ['072', 2], + 'BYN' => ['933', 2], + 'BZD' => ['084', 2], + 'CAD' => ['124', 2], + 'CDF' => ['976', 2], + 'CHF' => ['756', 2], + 'CLP' => ['152', 0], + 'CNY' => ['156', 2], + 'COP' => ['170', 2], + 'CRC' => ['188', 2], + 'CUC' => ['931', 2], + 'CUP' => ['192', 2], + 'CVE' => ['132', 2], + 'CZK' => ['203', 2], + 'DJF' => ['262', 0], + 'DKK' => ['208', 2], + 'DOP' => ['214', 2], + 'DZD' => ['012', 2], + 'EGP' => ['818', 2], + 'ERN' => ['232', 2], + 'ETB' => ['230', 2], + 'EUR' => ['978', 2], + 'FJD' => ['242', 2], + 'FKP' => ['238', 2], + 'GBP' => ['826', 2], + 'GEL' => ['981', 2], + 'GHS' => ['936', 2], + 'GIP' => ['292', 2], + 'GMD' => ['270', 2], + 'GNF' => ['324', 0], + 'GTQ' => ['320', 2], + 'GYD' => ['328', 2], + 'HKD' => ['344', 2], + 'HNL' => ['340', 2], + 'HRK' => ['191', 2], + 'HTG' => ['332', 2], + 'HUF' => ['348', 2], + 'IDR' => ['360', 2], + 'ILS' => ['376', 2], + 'INR' => ['356', 2], + 'IQD' => ['368', 0], + 'IRR' => ['364', 0], + 'ISK' => ['352', 0], + 'JMD' => ['388', 2], + 'JOD' => ['400', 3], + 'JPY' => ['392', 0], + 'KES' => ['404', 2], + 'KGS' => ['417', 2], + 'KHR' => ['116', 2], + 'KMF' => ['174', 0], + 'KPW' => ['408', 0], + 'KRW' => ['410', 0], + 'KWD' => ['414', 3], + 'KYD' => ['136', 2], + 'KZT' => ['398', 2], + 'LAK' => ['418', 0], + 'LBP' => ['422', 0], + 'LKR' => ['144', 2], + 'LRD' => ['430', 2], + 'LSL' => ['426', 2], + 'LYD' => ['434', 3], + 'MAD' => ['504', 2], + 'MDL' => ['498', 2], + 'MGA' => ['969', 0], + 'MKD' => ['807', 2], + 'MMK' => ['104', 0], + 'MNT' => ['496', 2], + 'MOP' => ['446', 2], + 'MRU' => ['929', 2], + 'MUR' => ['480', 2], + 'MVR' => ['462', 2], + 'MWK' => ['454', 2], + 'MXN' => ['484', 2], + 'MYR' => ['458', 2], + 'MZN' => ['943', 2], + 'NAD' => ['516', 2], + 'NGN' => ['566', 2], + 'NIO' => ['558', 2], + 'NOK' => ['578', 2], + 'NPR' => ['524', 2], + 'NZD' => ['554', 2], + 'OMR' => ['512', 3], + 'PAB' => ['590', 2], + 'PEN' => ['604', 2], + 'PGK' => ['598', 2], + 'PHP' => ['608', 2], + 'PKR' => ['586', 2], + 'PLN' => ['985', 2], + 'PYG' => ['600', 0], + 'QAR' => ['634', 2], + 'RON' => ['946', 2], + 'RSD' => ['941', 0], + 'RUB' => ['643', 2], + 'RWF' => ['646', 0], + 'SAR' => ['682', 2], + 'SBD' => ['090', 2], + 'SCR' => ['690', 2], + 'SDG' => ['938', 2], + 'SEK' => ['752', 2], + 'SGD' => ['702', 2], + 'SHP' => ['654', 2], + 'SLL' => ['694', 0], + 'SOS' => ['706', 0], + 'SRD' => ['968', 2], + 'SSP' => ['728', 2], + 'STN' => ['930', 2], + 'SVC' => ['222', 2], + 'SYP' => ['760', 0], + 'SZL' => ['748', 2], + 'THB' => ['764', 2], + 'TJS' => ['972', 2], + 'TMT' => ['934', 2], + 'TND' => ['788', 3], + 'TOP' => ['776', 2], + 'TRY' => ['949', 2], + 'TTD' => ['780', 2], + 'TWD' => ['901', 2], + 'TZS' => ['834', 2], + 'UAH' => ['980', 2], + 'UGX' => ['800', 0], + 'USD' => ['840', 2], + 'UYU' => ['858', 2], + 'UYW' => ['927', 4], + 'UZS' => ['860', 2], + 'VES' => ['928', 2], + 'VND' => ['704', 0], + 'VUV' => ['548', 0], + 'WST' => ['882', 2], + 'XAF' => ['950', 0], + 'XCD' => ['951', 2], + 'XOF' => ['952', 0], + 'XPF' => ['953', 0], + 'YER' => ['886', 0], + 'ZAR' => ['710', 2], + 'ZMW' => ['967', 2], + 'ZWL' => ['932', 2], + ]; } } diff --git a/vendor/commerceguys/intl/src/Currency/CurrencyRepositoryInterface.php b/vendor/commerceguys/intl/src/Currency/CurrencyRepositoryInterface.php index f0a091969..eb726ddb8 100644 --- a/vendor/commerceguys/intl/src/Currency/CurrencyRepositoryInterface.php +++ b/vendor/commerceguys/intl/src/Currency/CurrencyRepositoryInterface.php @@ -8,34 +8,30 @@ namespace CommerceGuys\Intl\Currency; interface CurrencyRepositoryInterface { /** - * Returns a currency instance matching the provided currency code. + * Gets a currency matching the provided currency code. * - * @param string $currencyCode The currency code. - * @param string $locale The locale (i.e. fr-FR). - * @param string $fallbackLocale A fallback locale (i.e "en"). + * @param string $currencyCode The currency code. + * @param string $locale The locale (i.e. fr-FR). * - * @return CurrencyInterface + * @return Currency */ - public function get($currencyCode, $locale = null, $fallbackLocale = null); + public function get($currencyCode, $locale = null); /** - * Returns all currency instances. + * Gets all currencies. * - * @param string $locale The locale (i.e. fr-FR). - * @param string $fallbackLocale A fallback locale (i.e "en"). + * @param string $locale The locale (i.e. fr-FR). * - * @return array An array of currencies implementing the CurrencyInterface, - * keyed by currency code. + * @return Currency[] An array of currencies, keyed by currency code. */ - public function getAll($locale = null, $fallbackLocale = null); + public function getAll($locale = null); /** - * Returns a list of currencies. + * Gets a list of currencies. * - * @param string $locale The locale (i.e. fr-FR). - * @param string $fallbackLocale A fallback locale (i.e "en"). + * @param string $locale The locale (i.e. fr-FR). * - * @return array An array of currency names, keyed by currency code. + * @return string[] An array of currency names, keyed by currency code. */ - public function getList($locale = null, $fallbackLocale = null); + public function getList($locale = null); } |