123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Directory\Model;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\Exception\InputException;
- use Magento\Directory\Model\Currency\Filter;
- /**
- * Currency model
- *
- * @api
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Currency extends \Magento\Framework\Model\AbstractModel
- {
- /**
- * CONFIG path constants
- */
- const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
- const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
- const XML_PATH_CURRENCY_BASE = 'currency/options/base';
- /**
- * @var Filter
- */
- protected $_filter;
- /**
- * Currency Rates
- *
- * @var array
- */
- protected $_rates;
- /**
- * @var \Magento\Framework\Locale\FormatInterface
- */
- protected $_localeFormat;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var \Magento\Directory\Helper\Data
- */
- protected $_directoryHelper;
- /**
- * @var \Magento\Directory\Model\Currency\FilterFactory
- */
- protected $_currencyFilterFactory;
- /**
- * @var \Magento\Framework\Locale\CurrencyInterface
- */
- protected $_localeCurrency;
- /**
- * @var CurrencyConfig
- */
- private $currencyConfig;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\Locale\FormatInterface $localeFormat
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Directory\Helper\Data $directoryHelper
- * @param Currency\FilterFactory $currencyFilterFactory
- * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- * @param CurrencyConfig $currencyConfig
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __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
- ) {
- parent::__construct(
- $context,
- $registry,
- $resource,
- $resourceCollection,
- $data
- );
- $this->_localeFormat = $localeFormat;
- $this->_storeManager = $storeManager;
- $this->_directoryHelper = $directoryHelper;
- $this->_currencyFilterFactory = $currencyFilterFactory;
- $this->_localeCurrency = $localeCurrency;
- $this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencyConfig::class);
- }
- /**
- * @return void
- */
- protected function _construct()
- {
- $this->_init(\Magento\Directory\Model\ResourceModel\Currency::class);
- }
- /**
- * Get currency code
- *
- * @return string
- */
- public function getCode()
- {
- return $this->_getData('currency_code');
- }
- /**
- * Get currency code
- *
- * @return string
- */
- public function getCurrencyCode()
- {
- return $this->_getData('currency_code');
- }
- /**
- * Currency Rates getter
- *
- * @return array
- */
- public function getRates()
- {
- return $this->_rates;
- }
- /**
- * Currency Rates setter
- *
- * @param array $rates Currency Rates
- * @return $this
- */
- public function setRates(array $rates)
- {
- $this->_rates = $rates;
- return $this;
- }
- /**
- * Loading currency data
- *
- * @param string $id
- * @param string $field
- * @return $this
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function load($id, $field = null)
- {
- $this->unsRate();
- $this->setData('currency_code', $id);
- return $this;
- }
- /**
- * Get currency rate (only base => allowed)
- *
- * @param mixed $toCurrency
- * @return float
- */
- public function getRate($toCurrency)
- {
- $code = $this->getCurrencyCodeFromToCurrency($toCurrency);
- $rates = $this->getRates();
- if (!isset($rates[$code])) {
- $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
- $this->setRates($rates);
- }
- return $rates[$code];
- }
- /**
- * Get currency rate (base=>allowed or allowed=>base)
- *
- * @param mixed $toCurrency
- * @return float
- */
- public function getAnyRate($toCurrency)
- {
- $code = $this->getCurrencyCodeFromToCurrency($toCurrency);
- $rates = $this->getRates();
- if (!isset($rates[$code])) {
- $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency);
- $this->setRates($rates);
- }
- return $rates[$code];
- }
- /**
- * Convert price to currency format
- *
- * @param float $price
- * @param mixed $toCurrency
- * @return float
- * @throws \Exception
- */
- public function convert($price, $toCurrency = null)
- {
- if ($toCurrency === null) {
- return $price;
- } elseif ($rate = $this->getRate($toCurrency)) {
- return (float)$price * (float)$rate;
- }
- throw new \Exception(__(
- 'Undefined rate from "%1-%2".',
- $this->getCode(),
- $this->getCurrencyCodeFromToCurrency($toCurrency)
- ));
- }
- /**
- * @param mixed $toCurrency
- * @return string
- * @throws \Magento\Framework\Exception\InputException
- */
- private function getCurrencyCodeFromToCurrency($toCurrency)
- {
- if (is_string($toCurrency)) {
- $code = $toCurrency;
- } elseif ($toCurrency instanceof \Magento\Directory\Model\Currency) {
- $code = $toCurrency->getCurrencyCode();
- } else {
- throw new InputException(__('Please correct the target currency.'));
- }
- return $code;
- }
- /**
- * Get currency filter
- *
- * @return Filter
- */
- public function getFilter()
- {
- if (!$this->_filter) {
- $this->_filter = $this->_currencyFilterFactory->create(['code' => $this->getCode()]);
- }
- return $this->_filter;
- }
- /**
- * Format price to currency format
- *
- * @param float $price
- * @param array $options
- * @param bool $includeContainer
- * @param bool $addBrackets
- * @return string
- */
- public function format($price, $options = [], $includeContainer = true, $addBrackets = false)
- {
- return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
- }
- /**
- * Apply currency format to number with specific rounding precision
- *
- * @param float $price
- * @param int $precision
- * @param array $options
- * @param bool $includeContainer
- * @param bool $addBrackets
- * @return string
- */
- public function formatPrecision(
- $price,
- $precision,
- $options = [],
- $includeContainer = true,
- $addBrackets = false
- ) {
- if (!isset($options['precision'])) {
- $options['precision'] = $precision;
- }
- if ($includeContainer) {
- return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt(
- $price,
- $options
- ) . ($addBrackets ? ']' : '') . '</span>';
- }
- return $this->formatTxt($price, $options);
- }
- /**
- * @param float $price
- * @param array $options
- * @return string
- */
- public function formatTxt($price, $options = [])
- {
- if (!is_numeric($price)) {
- $price = $this->_localeFormat->getNumber($price);
- }
- /**
- * 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).
- */
- $price = sprintf("%F", $price);
- return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
- }
- /**
- * Return currency symbol for current locale and currency code
- *
- * @return string
- */
- public function getCurrencySymbol()
- {
- return $this->_localeCurrency->getCurrency($this->getCode())->getSymbol();
- }
- /**
- * @return string
- */
- public function getOutputFormat()
- {
- $formatted = $this->formatTxt(0);
- $number = $this->formatTxt(0, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
- return str_replace($this->trimUnicodeDirectionMark($number), '%s', $formatted);
- }
- /**
- * Retrieve allowed currencies according to config
- *
- * @return array
- */
- public function getConfigAllowCurrencies()
- {
- $allowedCurrencies = $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_ALLOW);
- $appBaseCurrencyCode = $this->_directoryHelper->getBaseCurrencyCode();
- if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
- $allowedCurrencies[] = $appBaseCurrencyCode;
- }
- foreach ($this->_storeManager->getStores() as $store) {
- $code = $store->getBaseCurrencyCode();
- if (!in_array($code, $allowedCurrencies)) {
- $allowedCurrencies[] = $code;
- }
- }
- return $allowedCurrencies;
- }
- /**
- * Retrieve default currencies according to config
- *
- * @return array
- */
- public function getConfigDefaultCurrencies()
- {
- return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
- }
- /**
- * @return array
- */
- public function getConfigBaseCurrencies()
- {
- return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
- }
- /**
- * Retrieve currency rates to other currencies
- *
- * @param string $currency
- * @param array|null $toCurrencies
- * @return array
- */
- public function getCurrencyRates($currency, $toCurrencies = null)
- {
- if ($currency instanceof \Magento\Directory\Model\Currency) {
- $currency = $currency->getCode();
- }
- $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies);
- return $data;
- }
- /**
- * Save currency rates
- *
- * @param array $rates
- * @return $this
- */
- public function saveRates($rates)
- {
- $this->_getResource()->saveRates($rates);
- return $this;
- }
- /**
- * This method removes LRM and RLM marks from string
- *
- * @param string $string
- * @return $this
- */
- private function trimUnicodeDirectionMark($string)
- {
- if (preg_match('/^(\x{200E}|\x{200F})/u', $string, $match)) {
- $string = preg_replace('/^'.$match[1].'/u', '', $string);
- }
- return $string;
- }
- }
|