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

Public Member Functions

 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Directory\Model\Currency\FilterFactory $currencyFilterFactory, \Magento\Framework\Locale\CurrencyInterface $localeCurrency, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], CurrencyConfig $currencyConfig=null)
 
 getCode ()
 
 getCurrencyCode ()
 
 getRates ()
 
 setRates (array $rates)
 
 load ($id, $field=null)
 
 getRate ($toCurrency)
 
 getAnyRate ($toCurrency)
 
 convert ($price, $toCurrency=null)
 
 getFilter ()
 
 format ($price, $options=[], $includeContainer=true, $addBrackets=false)
 
 formatPrecision ( $price, $precision, $options=[], $includeContainer=true, $addBrackets=false)
 
 formatTxt ($price, $options=[])
 
 getCurrencySymbol ()
 
 getOutputFormat ()
 
 getConfigAllowCurrencies ()
 
 getConfigDefaultCurrencies ()
 
 getConfigBaseCurrencies ()
 
 getCurrencyRates ($currency, $toCurrencies=null)
 
 saveRates ($rates)
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Data Fields

const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow'
 
const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default'
 
const XML_PATH_CURRENCY_BASE = 'currency/options/base'
 

Protected Member Functions

 _construct ()
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_filter
 
 $_rates
 
 $_localeFormat
 
 $_storeManager
 
 $_directoryHelper
 
 $_currencyFilterFactory
 
 $_localeCurrency
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Currency model

@api

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 21 of file Currency.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\Context  $context,
\Magento\Framework\Registry  $registry,
\Magento\Framework\Locale\FormatInterface  $localeFormat,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Directory\Helper\Data  $directoryHelper,
\Magento\Directory\Model\Currency\FilterFactory  $currencyFilterFactory,
\Magento\Framework\Locale\CurrencyInterface  $localeCurrency,
\Magento\Framework\Model\ResourceModel\AbstractResource  $resource = null,
\Magento\Framework\Data\Collection\AbstractDb  $resourceCollection = null,
array  $data = [],
CurrencyConfig  $currencyConfig = null 
)
Parameters
\Magento\Framework\Model\Context$context
\Magento\Framework\Registry$registry
\Magento\Framework\Locale\FormatInterface$localeFormat
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Directory\Helper\Data$directoryHelper
Currency\FilterFactory$currencyFilterFactory
\Magento\Framework\Locale\CurrencyInterface$localeCurrency
\Magento\Framework\Model\ResourceModel\AbstractResource$resource
\Magento\Framework\Data\Collection\AbstractDb$resourceCollection
array$data
CurrencyConfig$currencyConfig@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 88 of file Currency.php.

100  {
101  parent::__construct(
102  $context,
103  $registry,
104  $resource,
105  $resourceCollection,
106  $data
107  );
108  $this->_localeFormat = $localeFormat;
109  $this->_storeManager = $storeManager;
110  $this->_directoryHelper = $directoryHelper;
111  $this->_currencyFilterFactory = $currencyFilterFactory;
112  $this->_localeCurrency = $localeCurrency;
113  $this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencyConfig::class);
114  }
$storeManager
$resource
Definition: bulk.php:12

Member Function Documentation

◆ _construct()

_construct ( )
protected
Returns
void

Definition at line 119 of file Currency.php.

120  {
121  $this->_init(\Magento\Directory\Model\ResourceModel\Currency::class);
122  }

◆ convert()

convert (   $price,
  $toCurrency = null 
)

Convert price to currency format

Parameters
float$price
mixed$toCurrency
Returns
float
Exceptions

Definition at line 223 of file Currency.php.

224  {
225  if ($toCurrency === null) {
226  return $price;
227  } elseif ($rate = $this->getRate($toCurrency)) {
228  return (float)$price * (float)$rate;
229  }
230 
231  throw new \Exception(__(
232  'Undefined rate from "%1-%2".',
233  $this->getCode(),
234  $this->getCurrencyCodeFromToCurrency($toCurrency)
235  ));
236  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$price

◆ format()

format (   $price,
  $options = [],
  $includeContainer = true,
  $addBrackets = false 
)

Format price to currency format

Parameters
float$price
array$options
bool$includeContainer
bool$addBrackets
Returns
string

Definition at line 278 of file Currency.php.

279  {
280  return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
281  }
$price
formatPrecision( $price, $precision, $options=[], $includeContainer=true, $addBrackets=false)
Definition: Currency.php:293

◆ formatPrecision()

formatPrecision (   $price,
  $precision,
  $options = [],
  $includeContainer = true,
  $addBrackets = false 
)

Apply currency format to number with specific rounding precision

Parameters
float$price
int$precision
array$options
bool$includeContainer
bool$addBrackets
Returns
string

Definition at line 293 of file Currency.php.

299  {
300  if (!isset($options['precision'])) {
301  $options['precision'] = $precision;
302  }
303  if ($includeContainer) {
304  return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt(
305  $price,
306  $options
307  ) . ($addBrackets ? ']' : '') . '</span>';
308  }
309  return $this->formatTxt($price, $options);
310  }
$price
formatTxt($price, $options=[])
Definition: Currency.php:317

◆ formatTxt()

formatTxt (   $price,
  $options = [] 
)
Parameters
float$price
array$options
Returns
string

Fix problem with 12 000 000, 1 200 000

f - the argument is treated as a float, and presented as a floating-point number (locale aware). F - the argument is treated as a float, and presented as a floating-point number (non-locale aware).

Definition at line 317 of file Currency.php.

318  {
319  if (!is_numeric($price)) {
320  $price = $this->_localeFormat->getNumber($price);
321  }
328  $price = sprintf("%F", $price);
329  return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
330  }
$price

◆ getAnyRate()

getAnyRate (   $toCurrency)

Get currency rate (base=>allowed or allowed=>base)

Parameters
mixed$toCurrency
Returns
float

Definition at line 204 of file Currency.php.

205  {
206  $code = $this->getCurrencyCodeFromToCurrency($toCurrency);
207  $rates = $this->getRates();
208  if (!isset($rates[$code])) {
209  $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency);
210  $this->setRates($rates);
211  }
212  return $rates[$code];
213  }
$rates
Definition: tax.phtml:35
$code
Definition: info.phtml:12

◆ getCode()

getCode ( )

Get currency code

Returns
string

Definition at line 129 of file Currency.php.

130  {
131  return $this->_getData('currency_code');
132  }

◆ getConfigAllowCurrencies()

getConfigAllowCurrencies ( )

Retrieve allowed currencies according to config

Returns
array

Definition at line 357 of file Currency.php.

358  {
359  $allowedCurrencies = $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_ALLOW);
360  $appBaseCurrencyCode = $this->_directoryHelper->getBaseCurrencyCode();
361  if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
362  $allowedCurrencies[] = $appBaseCurrencyCode;
363  }
364  foreach ($this->_storeManager->getStores() as $store) {
365  $code = $store->getBaseCurrencyCode();
366  if (!in_array($code, $allowedCurrencies)) {
367  $allowedCurrencies[] = $code;
368  }
369  }
370 
371  return $allowedCurrencies;
372  }
$code
Definition: info.phtml:12

◆ getConfigBaseCurrencies()

getConfigBaseCurrencies ( )
Returns
array

Definition at line 387 of file Currency.php.

388  {
389  return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
390  }

◆ getConfigDefaultCurrencies()

getConfigDefaultCurrencies ( )

Retrieve default currencies according to config

Returns
array

Definition at line 379 of file Currency.php.

380  {
381  return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
382  }

◆ getCurrencyCode()

getCurrencyCode ( )

Get currency code

Returns
string

Definition at line 139 of file Currency.php.

140  {
141  return $this->_getData('currency_code');
142  }

◆ getCurrencyRates()

getCurrencyRates (   $currency,
  $toCurrencies = null 
)

Retrieve currency rates to other currencies

Parameters
string$currency
array | null$toCurrencies
Returns
array

Definition at line 399 of file Currency.php.

400  {
401  if ($currency instanceof \Magento\Directory\Model\Currency) {
402  $currency = $currency->getCode();
403  }
404  $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies);
405  return $data;
406  }

◆ getCurrencySymbol()

getCurrencySymbol ( )

Return currency symbol for current locale and currency code

Returns
string

Definition at line 337 of file Currency.php.

338  {
339  return $this->_localeCurrency->getCurrency($this->getCode())->getSymbol();
340  }

◆ getFilter()

getFilter ( )

Get currency filter

Returns
Filter

Definition at line 260 of file Currency.php.

261  {
262  if (!$this->_filter) {
263  $this->_filter = $this->_currencyFilterFactory->create(['code' => $this->getCode()]);
264  }
265 
266  return $this->_filter;
267  }

◆ getOutputFormat()

getOutputFormat ( )
Returns
string

Definition at line 345 of file Currency.php.

346  {
347  $formatted = $this->formatTxt(0);
348  $number = $this->formatTxt(0, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
349  return str_replace($this->trimUnicodeDirectionMark($number), '%s', $formatted);
350  }
$number
Definition: details.phtml:22
formatTxt($price, $options=[])
Definition: Currency.php:317

◆ getRate()

getRate (   $toCurrency)

Get currency rate (only base => allowed)

Parameters
mixed$toCurrency
Returns
float

Definition at line 187 of file Currency.php.

188  {
189  $code = $this->getCurrencyCodeFromToCurrency($toCurrency);
190  $rates = $this->getRates();
191  if (!isset($rates[$code])) {
192  $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
193  $this->setRates($rates);
194  }
195  return $rates[$code];
196  }
$rates
Definition: tax.phtml:35
$code
Definition: info.phtml:12

◆ getRates()

getRates ( )

Currency Rates getter

Returns
array

Definition at line 149 of file Currency.php.

150  {
151  return $this->_rates;
152  }

◆ load()

load (   $id,
  $field = null 
)

Loading currency data

Parameters
string$id
string$field
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 174 of file Currency.php.

175  {
176  $this->unsRate();
177  $this->setData('currency_code', $id);
178  return $this;
179  }
$id
Definition: fieldset.phtml:14

◆ saveRates()

saveRates (   $rates)

Save currency rates

Parameters
array$rates
Returns
$this

Definition at line 414 of file Currency.php.

415  {
416  $this->_getResource()->saveRates($rates);
417  return $this;
418  }
$rates
Definition: tax.phtml:35

◆ setRates()

setRates ( array  $rates)

Currency Rates setter

Parameters
array$ratesCurrency Rates
Returns
$this

Definition at line 160 of file Currency.php.

161  {
162  $this->_rates = $rates;
163  return $this;
164  }
$rates
Definition: tax.phtml:35

Field Documentation

◆ $_currencyFilterFactory

$_currencyFilterFactory
protected

Definition at line 62 of file Currency.php.

◆ $_directoryHelper

$_directoryHelper
protected

Definition at line 57 of file Currency.php.

◆ $_filter

$_filter
protected

Definition at line 35 of file Currency.php.

◆ $_localeCurrency

$_localeCurrency
protected

Definition at line 67 of file Currency.php.

◆ $_localeFormat

$_localeFormat
protected

Definition at line 47 of file Currency.php.

◆ $_rates

$_rates
protected

Definition at line 42 of file Currency.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 52 of file Currency.php.

◆ XML_PATH_CURRENCY_ALLOW

const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow'

CONFIG path constants

Definition at line 26 of file Currency.php.

◆ XML_PATH_CURRENCY_BASE

const XML_PATH_CURRENCY_BASE = 'currency/options/base'

Definition at line 30 of file Currency.php.

◆ XML_PATH_CURRENCY_DEFAULT

const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default'

Definition at line 28 of file Currency.php.


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