Tax.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Block\Element\Weee;
  7. use \Magento\Framework\Currency;
  8. class Tax extends \Magento\Framework\Data\Form\Element\AbstractElement
  9. {
  10. /**
  11. * @var \Magento\Store\Model\StoreManagerInterface
  12. */
  13. protected $storeManager;
  14. /**
  15. * @var \Magento\Framework\Locale\CurrencyInterface
  16. */
  17. protected $localeCurrency;
  18. /**
  19. * @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
  20. * @param \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection
  21. * @param \Magento\Framework\Escaper $escaper
  22. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  23. * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
  24. * @param array $data
  25. */
  26. public function __construct(
  27. \Magento\Framework\Data\Form\Element\Factory $factoryElement,
  28. \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
  29. \Magento\Framework\Escaper $escaper,
  30. \Magento\Store\Model\StoreManagerInterface $storeManager,
  31. \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
  32. array $data = []
  33. ) {
  34. $this->localeCurrency = $localeCurrency;
  35. $this->storeManager = $storeManager;
  36. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  37. }
  38. /**
  39. * @return void
  40. */
  41. protected function _construct()
  42. {
  43. parent::_construct();
  44. }
  45. /**
  46. * @param mixed $attribute
  47. * @return \Magento\Store\Model\Store
  48. */
  49. protected function getStore($attribute)
  50. {
  51. if (!($storeId = $attribute->getStoreId())) {
  52. $storeId = $this->getForm()->getDataObject()->getStoreId();
  53. }
  54. $store = $this->storeManager->getStore($storeId);
  55. return $store;
  56. }
  57. /**
  58. * @param null|int|string $index
  59. * @return null|string
  60. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  61. */
  62. public function getEscapedValue($index = null)
  63. {
  64. $values = $this->getValue();
  65. if (!is_array($values)) {
  66. return null;
  67. }
  68. foreach ($values as $key => $value) {
  69. $price = array_key_exists('price', $value) ? $value['price'] : $value['value'];
  70. try {
  71. if ($attribute = $this->getEntityAttribute()) {
  72. $store = $this->getStore($attribute);
  73. $currency = $this->localeCurrency->getCurrency($store->getBaseCurrencyCode());
  74. $values[$key]['value'] = $currency->toCurrency($price, ['display' => Currency::NO_SYMBOL]);
  75. } else {
  76. // default format: 1234.56
  77. $values[$key]['value'] = number_format($price, 2, null, '');
  78. }
  79. } catch (\Exception $e) {
  80. $values[$key]['value'] = $price;
  81. }
  82. }
  83. return $values;
  84. }
  85. }