AddressFormatter.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Sales\ViewModel\Customer;
  8. use Magento\Framework\View\Element\Block\ArgumentInterface;
  9. /**
  10. * Customer address formatter
  11. */
  12. class AddressFormatter implements ArgumentInterface
  13. {
  14. /**
  15. * Customer form factory
  16. *
  17. * @var \Magento\Customer\Model\Metadata\FormFactory
  18. */
  19. private $customerFormFactory;
  20. /**
  21. * Address format helper
  22. *
  23. * @var \Magento\Customer\Helper\Address
  24. */
  25. private $addressFormatHelper;
  26. /**
  27. * Directory helper
  28. *
  29. * @var \Magento\Directory\Helper\Data
  30. */
  31. private $directoryHelper;
  32. /**
  33. * Session quote
  34. *
  35. * @var \Magento\Backend\Model\Session\Quote
  36. */
  37. private $session;
  38. /**
  39. * Json encoder
  40. *
  41. * @var \Magento\Framework\Serialize\Serializer\Json
  42. */
  43. private $jsonEncoder;
  44. /**
  45. * Customer address
  46. *
  47. * @param \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory
  48. * @param \Magento\Customer\Helper\Address $addressFormatHelper
  49. * @param \Magento\Directory\Helper\Data $directoryHelper
  50. * @param \Magento\Backend\Model\Session\Quote $session
  51. * @param \Magento\Framework\Serialize\Serializer\Json $jsonEncoder
  52. */
  53. public function __construct(
  54. \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory,
  55. \Magento\Customer\Helper\Address $addressFormatHelper,
  56. \Magento\Directory\Helper\Data $directoryHelper,
  57. \Magento\Backend\Model\Session\Quote $session,
  58. \Magento\Framework\Serialize\Serializer\Json $jsonEncoder
  59. ) {
  60. $this->customerFormFactory = $customerFormFactory;
  61. $this->addressFormatHelper = $addressFormatHelper;
  62. $this->directoryHelper = $directoryHelper;
  63. $this->session = $session;
  64. $this->jsonEncoder = $jsonEncoder;
  65. }
  66. /**
  67. * Return customer address array as JSON
  68. *
  69. * @param array $addressArray
  70. *
  71. * @return string
  72. */
  73. public function getAddressesJson(array $addressArray): string
  74. {
  75. $data = $this->getEmptyAddressForm();
  76. foreach ($addressArray as $addressId => $address) {
  77. $addressForm = $this->customerFormFactory->create(
  78. 'customer_address',
  79. 'adminhtml_customer_address',
  80. $address
  81. );
  82. $data[$addressId] = $addressForm->outputData(
  83. \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON
  84. );
  85. }
  86. return $this->jsonEncoder->serialize($data);
  87. }
  88. /**
  89. * Represent customer address in 'online' format.
  90. *
  91. * @param array $address
  92. * @return string
  93. */
  94. public function getAddressAsString(array $address): string
  95. {
  96. $formatTypeRenderer = $this->addressFormatHelper->getFormatTypeRenderer('oneline');
  97. $result = '';
  98. if ($formatTypeRenderer) {
  99. $result = $formatTypeRenderer->renderArray($address);
  100. }
  101. return $result;
  102. }
  103. /**
  104. * Return empty address address form
  105. *
  106. * @return array
  107. */
  108. private function getEmptyAddressForm(): array
  109. {
  110. $defaultCountryId = $this->directoryHelper->getDefaultCountry($this->session->getStore());
  111. $emptyAddressForm = $this->customerFormFactory->create(
  112. 'customer_address',
  113. 'adminhtml_customer_address',
  114. [\Magento\Customer\Api\Data\AddressInterface::COUNTRY_ID => $defaultCountryId]
  115. );
  116. return [0 => $emptyAddressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON)];
  117. }
  118. }