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
8 
12 
22 {
26  const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
27 
28  const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
29 
30  const XML_PATH_CURRENCY_BASE = 'currency/options/base';
31 
35  protected $_filter;
36 
42  protected $_rates;
43 
47  protected $_localeFormat;
48 
52  protected $_storeManager;
53 
57  protected $_directoryHelper;
58 
63 
67  protected $_localeCurrency;
68 
72  private $currencyConfig;
73 
88  public function __construct(
89  \Magento\Framework\Model\Context $context,
90  \Magento\Framework\Registry $registry,
91  \Magento\Framework\Locale\FormatInterface $localeFormat,
93  \Magento\Directory\Helper\Data $directoryHelper,
94  \Magento\Directory\Model\Currency\FilterFactory $currencyFilterFactory,
95  \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
96  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
97  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
98  array $data = [],
99  CurrencyConfig $currencyConfig = null
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  }
115 
119  protected function _construct()
120  {
121  $this->_init(\Magento\Directory\Model\ResourceModel\Currency::class);
122  }
123 
129  public function getCode()
130  {
131  return $this->_getData('currency_code');
132  }
133 
139  public function getCurrencyCode()
140  {
141  return $this->_getData('currency_code');
142  }
143 
149  public function getRates()
150  {
151  return $this->_rates;
152  }
153 
160  public function setRates(array $rates)
161  {
162  $this->_rates = $rates;
163  return $this;
164  }
165 
174  public function load($id, $field = null)
175  {
176  $this->unsRate();
177  $this->setData('currency_code', $id);
178  return $this;
179  }
180 
187  public function getRate($toCurrency)
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  }
197 
204  public function getAnyRate($toCurrency)
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  }
214 
223  public function convert($price, $toCurrency = null)
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  }
237 
243  private function getCurrencyCodeFromToCurrency($toCurrency)
244  {
245  if (is_string($toCurrency)) {
246  $code = $toCurrency;
247  } elseif ($toCurrency instanceof \Magento\Directory\Model\Currency) {
248  $code = $toCurrency->getCurrencyCode();
249  } else {
250  throw new InputException(__('Please correct the target currency.'));
251  }
252  return $code;
253  }
254 
260  public function getFilter()
261  {
262  if (!$this->_filter) {
263  $this->_filter = $this->_currencyFilterFactory->create(['code' => $this->getCode()]);
264  }
265 
266  return $this->_filter;
267  }
268 
278  public function format($price, $options = [], $includeContainer = true, $addBrackets = false)
279  {
280  return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
281  }
282 
293  public function formatPrecision(
294  $price,
295  $precision,
296  $options = [],
297  $includeContainer = true,
298  $addBrackets = false
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  }
311 
317  public function formatTxt($price, $options = [])
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  }
331 
337  public function getCurrencySymbol()
338  {
339  return $this->_localeCurrency->getCurrency($this->getCode())->getSymbol();
340  }
341 
345  public function getOutputFormat()
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  }
351 
357  public function getConfigAllowCurrencies()
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  }
373 
379  public function getConfigDefaultCurrencies()
380  {
381  return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
382  }
383 
387  public function getConfigBaseCurrencies()
388  {
389  return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
390  }
391 
399  public function getCurrencyRates($currency, $toCurrencies = null)
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  }
407 
414  public function saveRates($rates)
415  {
416  $this->_getResource()->saveRates($rates);
417  return $this;
418  }
419 
426  private function trimUnicodeDirectionMark($string)
427  {
428  if (preg_match('/^(\x{200E}|\x{200F})/u', $string, $match)) {
429  $string = preg_replace('/^'.$match[1].'/u', '', $string);
430  }
431  return $string;
432  }
433 }
__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)
Definition: Currency.php:88
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
format($price, $options=[], $includeContainer=true, $addBrackets=false)
Definition: Currency.php:278
$number
Definition: details.phtml:22
$id
Definition: fieldset.phtml:14
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$price
$rates
Definition: tax.phtml:35
getCurrencyRates($currency, $toCurrencies=null)
Definition: Currency.php:399
formatPrecision( $price, $precision, $options=[], $includeContainer=true, $addBrackets=false)
Definition: Currency.php:293
formatTxt($price, $options=[])
Definition: Currency.php:317
convert($price, $toCurrency=null)
Definition: Currency.php:223
$code
Definition: info.phtml:12