Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes | Static Protected Attributes
Currency Class Reference
Inheritance diagram for Currency:
CurrencyInterface

Public Member Functions

 __construct (\Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Framework\CurrencyFactory $currencyFactory)
 
 getDefaultCurrency ()
 
 getCurrency ($currency)
 

Data Fields

const DEFAULT_CURRENCY = 'USD'
 
const CURRENCY_OPTION_SYMBOL = 'symbol'
 
const CURRENCY_OPTION_CURRENCY = 'currency'
 
const CURRENCY_OPTION_NAME = 'name'
 
const CURRENCY_OPTION_DISPLAY = 'display'
 

Protected Attributes

 $_eventManager = null
 
 $_localeResolver
 
 $_currencyFactory
 

Static Protected Attributes

static $_currencyCache = []
 

Detailed Description

Definition at line 8 of file Currency.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Event\ManagerInterface  $eventManager,
\Magento\Framework\Locale\ResolverInterface  $localeResolver,
\Magento\Framework\CurrencyFactory  $currencyFactory 
)
Parameters
\Magento\Framework\Event\ManagerInterface$eventManager
ResolverInterface$localeResolver
\Magento\Framework\CurrencyFactory$currencyFactory

Definition at line 53 of file Currency.php.

57  {
58  $this->_eventManager = $eventManager;
59  $this->_localeResolver = $localeResolver;
60  $this->_currencyFactory = $currencyFactory;
61  }

Member Function Documentation

◆ getCurrency()

getCurrency (   $currency)

Create Currency object for current locale

Parameters
string$currency
Returns
\Magento\Framework\Currency

Implements CurrencyInterface.

Definition at line 74 of file Currency.php.

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  }

◆ getDefaultCurrency()

getDefaultCurrency ( )

Retrieve default currency code

Returns
string

Implements CurrencyInterface.

Definition at line 66 of file Currency.php.

67  {
69  }

Field Documentation

◆ $_currencyCache

$_currencyCache = []
staticprotected

Definition at line 29 of file Currency.php.

◆ $_currencyFactory

$_currencyFactory
protected

Definition at line 46 of file Currency.php.

◆ $_eventManager

$_eventManager = null
protected

Definition at line 36 of file Currency.php.

◆ $_localeResolver

$_localeResolver
protected

Definition at line 41 of file Currency.php.

◆ CURRENCY_OPTION_CURRENCY

const CURRENCY_OPTION_CURRENCY = 'currency'

Definition at line 20 of file Currency.php.

◆ CURRENCY_OPTION_DISPLAY

const CURRENCY_OPTION_DISPLAY = 'display'

Definition at line 24 of file Currency.php.

◆ CURRENCY_OPTION_NAME

const CURRENCY_OPTION_NAME = 'name'

Definition at line 22 of file Currency.php.

◆ CURRENCY_OPTION_SYMBOL

const CURRENCY_OPTION_SYMBOL = 'symbol'

#+ Currency Options

Definition at line 18 of file Currency.php.

◆ DEFAULT_CURRENCY

const DEFAULT_CURRENCY = 'USD'

Default currency

Definition at line 13 of file Currency.php.


The documentation for this class was generated from the following file: