CurrencyDisplayOptions.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CurrencySymbol\Observer;
  7. use Magento\Framework\Locale\Currency;
  8. use Magento\Framework\Event\ObserverInterface;
  9. class CurrencyDisplayOptions implements ObserverInterface
  10. {
  11. /**
  12. * @var \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory
  13. */
  14. protected $symbolFactory;
  15. /**
  16. * @param \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory
  17. */
  18. public function __construct(\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory)
  19. {
  20. $this->symbolFactory = $symbolFactory;
  21. }
  22. /**
  23. * Generate options for currency displaying with custom currency symbol
  24. *
  25. * @param \Magento\Framework\Event\Observer $observer
  26. * @return $this
  27. */
  28. public function execute(\Magento\Framework\Event\Observer $observer)
  29. {
  30. $baseCode = $observer->getEvent()->getBaseCode();
  31. $currencyOptions = $observer->getEvent()->getCurrencyOptions();
  32. $currencyOptions->addData($this->getCurrencyOptions($baseCode));
  33. return $this;
  34. }
  35. /**
  36. * Get currency display options
  37. *
  38. * @param string $baseCode
  39. * @return array
  40. */
  41. protected function getCurrencyOptions($baseCode)
  42. {
  43. $currencyOptions = [];
  44. if ($baseCode) {
  45. $customCurrencySymbol = $this->symbolFactory->create()->getCurrencySymbol($baseCode);
  46. if ($customCurrencySymbol) {
  47. $currencyOptions[Currency::CURRENCY_OPTION_SYMBOL] = $customCurrencySymbol;
  48. $currencyOptions[Currency::CURRENCY_OPTION_DISPLAY] = \Magento\Framework\Currency::USE_SYMBOL;
  49. }
  50. }
  51. return $currencyOptions;
  52. }
  53. }