Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Currency.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  const DEFAULT_CURRENCY = 'USD';
14 
18  const CURRENCY_OPTION_SYMBOL = 'symbol';
19 
20  const CURRENCY_OPTION_CURRENCY = 'currency';
21 
22  const CURRENCY_OPTION_NAME = 'name';
23 
24  const CURRENCY_OPTION_DISPLAY = 'display';
25 
29  protected static $_currencyCache = [];
30 
36  protected $_eventManager = null;
37 
41  protected $_localeResolver;
42 
46  protected $_currencyFactory;
47 
53  public function __construct(
54  \Magento\Framework\Event\ManagerInterface $eventManager,
55  \Magento\Framework\Locale\ResolverInterface $localeResolver,
56  \Magento\Framework\CurrencyFactory $currencyFactory
57  ) {
58  $this->_eventManager = $eventManager;
59  $this->_localeResolver = $localeResolver;
60  $this->_currencyFactory = $currencyFactory;
61  }
62 
66  public function getDefaultCurrency()
67  {
69  }
70 
74  public function getCurrency($currency)
75  {
76  \Magento\Framework\Profiler::start('locale/currency');
77  if (!isset(self::$_currencyCache[$this->_localeResolver->getLocale()][$currency])) {
78  $options = [];
79  try {
80  $currencyObject = $this->_currencyFactory->create(
81  ['options' => $currency, 'locale' => $this->_localeResolver->getLocale()]
82  );
83  } catch (\Exception $e) {
84  $currencyObject = $this->_currencyFactory->create(
85  ['options' => $this->getDefaultCurrency(), 'locale' => $this->_localeResolver->getLocale()]
86  );
90  }
91 
92  $options = new \Magento\Framework\DataObject($options);
93  $this->_eventManager->dispatch(
94  'currency_display_options_forming',
95  ['currency_options' => $options, 'base_code' => $currency]
96  );
97 
98  $currencyObject->setFormat($options->toArray());
99  self::$_currencyCache[$this->_localeResolver->getLocale()][$currency] = $currencyObject;
100  }
101  \Magento\Framework\Profiler::stop('locale/currency');
102  return self::$_currencyCache[$this->_localeResolver->getLocale()][$currency];
103  }
104 }
__construct(\Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Framework\CurrencyFactory $currencyFactory)
Definition: Currency.php:53