Data.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Helper;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. /**
  9. * Pricing data helper
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  15. {
  16. /**
  17. * @var PriceCurrencyInterface
  18. */
  19. protected $priceCurrency;
  20. /**
  21. * @param \Magento\Framework\App\Helper\Context $context
  22. * @param PriceCurrencyInterface $priceCurrency
  23. */
  24. public function __construct(
  25. \Magento\Framework\App\Helper\Context $context,
  26. PriceCurrencyInterface $priceCurrency
  27. ) {
  28. parent::__construct($context);
  29. $this->priceCurrency = $priceCurrency;
  30. }
  31. /**
  32. * Convert and format price value for current application store
  33. *
  34. * @param float $value
  35. * @param bool $format
  36. * @param bool $includeContainer
  37. * @return float|string
  38. */
  39. public function currency($value, $format = true, $includeContainer = true)
  40. {
  41. return $format
  42. ? $this->priceCurrency->convertAndFormat($value, $includeContainer)
  43. : $this->priceCurrency->convert($value);
  44. }
  45. /**
  46. * Convert and format price value for specified store
  47. *
  48. * @param float $value
  49. * @param int|\Magento\Store\Model\Store $store
  50. * @param bool $format
  51. * @param bool $includeContainer
  52. * @return float|string
  53. */
  54. public function currencyByStore($value, $store = null, $format = true, $includeContainer = true)
  55. {
  56. if ($format) {
  57. $value = $this->priceCurrency->convertAndFormat(
  58. $value,
  59. $includeContainer,
  60. PriceCurrencyInterface::DEFAULT_PRECISION,
  61. $store
  62. );
  63. } else {
  64. $value = $this->priceCurrency->convert($value, $store);
  65. }
  66. return $value;
  67. }
  68. }