TotalsConverter.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Cart;
  7. use Magento\Quote\Api\Data\TotalSegmentInterface;
  8. use Magento\Quote\Api\Data\TotalSegmentInterfaceFactory;
  9. /**
  10. * Cart totals data objects converter.
  11. */
  12. class TotalsConverter
  13. {
  14. /**
  15. * @var TotalSegmentInterfaceFactory
  16. */
  17. protected $factory;
  18. /**
  19. * @param TotalSegmentInterfaceFactory $factory
  20. */
  21. public function __construct(
  22. TotalSegmentInterfaceFactory $factory
  23. ) {
  24. $this->factory = $factory;
  25. }
  26. /**
  27. * @param \Magento\Quote\Model\Quote\Address\Total[] $addressTotals
  28. * @return \Magento\Quote\Api\Data\TotalSegmentInterface[]
  29. */
  30. public function process($addressTotals)
  31. {
  32. $data = [];
  33. /** @var \Magento\Quote\Model\Quote\Address\Total $addressTotal */
  34. foreach ($addressTotals as $addressTotal) {
  35. $pureData = [
  36. TotalSegmentInterface::CODE => $addressTotal->getCode(),
  37. TotalSegmentInterface::TITLE => '',
  38. TotalSegmentInterface::VALUE => $addressTotal->getValue(),
  39. TotalSegmentInterface::AREA => $addressTotal->getArea(),
  40. ];
  41. if (is_object($addressTotal->getTitle())) {
  42. $pureData[TotalSegmentInterface::TITLE] = $addressTotal->getTitle()->render();
  43. }
  44. /** @var \Magento\Quote\Model\Cart\TotalSegment $total */
  45. $total = $this->factory->create();
  46. $total->setData($pureData);
  47. $data[$addressTotal->getCode()] = $total;
  48. }
  49. return $data;
  50. }
  51. }