-
Notifications
You must be signed in to change notification settings - Fork 12
/
currency.php
79 lines (73 loc) · 2.29 KB
/
currency.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
use Magento\Framework\Locale\Bundle\CurrencyBundle;
use Magento\Store\Api\Data\StoreInterface;
/**
* 2015-12-28
* @param int|string|null|bool|StoreInterface $store [optional]
* @return string[]
*/
function df_currencies_codes_allowed($store = null) {
return df_store($store)->getAvailableCurrencyCodes(true);
}
/**
* 2016-06-30
* «How to programmatically check whether a currency is allowed
* and has an exchange rate to the base currency?» https://mage2.pro/t/1832
* @param string $iso3
* @param int|string|null|bool|StoreInterface $store [optional]
* @return string[]
*/
function df_currency_has_rate($iso3, $store = null) {return !!dfa(df_currencies_ctn($store), $iso3);}
/**
* 2015-12-28
* @param int|string|null|bool|StoreInterface $store [optional]
* @return array(string => string)
*/
function df_currencies_ctn($store = null) {
$store = df_store($store);
/** @var array(int => array(string => string)) */
static $cache;
/** @var string $cacheKey */
$cacheKey = $store->getId();
if (!isset($cache[$cacheKey])) {
/** @var \Magento\Directory\Model\Currency $currency */
$currency = df_o(\Magento\Directory\Model\Currency::class);
/** @var string[] $codes */
$codes = df_currencies_codes_allowed($store);
// 2016-02-17
// $rates ниже не содержит базовую валюту.
/** @var string $baseCode */
$baseCode = $store->getBaseCurrency()->getCode();
/** @var array(string => float) $rates */
$rates = $currency->getCurrencyRates($store->getBaseCurrency(), $codes);
/** @var array(string => string) $result */
$result = [];
foreach ($codes as $code) {
/** @var string $code */
if ($baseCode === $code || isset($rates[$code])) {
$result[$code] = df_currency_ctn($code);
}
}
$cache[$cacheKey] = $result;
}
return $cache[$cacheKey];
}
/**
* 2016-06-30
* @param string $iso3
* @return string
*/
function df_currency_ctn($iso3) {
/** @var \ResourceBundle $cache */
static $cache;
if (!isset($cache)) {
$cache = (new CurrencyBundle())->get(df_locale())['Currencies'];
}
return $cache[$iso3][1] ?: $iso3;
}
/**
* 2015-12-28
* @param int|string|null|bool|StoreInterface $store [optional]
* @return array(array(string => string))
*/
function df_currencies_options($store = null) {return df_map_to_options(df_currencies_ctn($store));}