|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Model\Customer; |
| 9 | + |
| 10 | +use Magento\Framework\DataObject; |
| 11 | +use Magento\Framework\View\Element\Block\ArgumentInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Customer address |
| 15 | + */ |
| 16 | +class Address extends DataObject implements ArgumentInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Customer addresses collection |
| 20 | + * |
| 21 | + * @var \Magento\Customer\Model\ResourceModel\Address\Collection |
| 22 | + */ |
| 23 | + private $addressCollection; |
| 24 | + |
| 25 | + /** |
| 26 | + * Customer address array |
| 27 | + * |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + private $addressArray; |
| 31 | + |
| 32 | + /** |
| 33 | + * Customer form factory |
| 34 | + * |
| 35 | + * @var \Magento\Customer\Model\Metadata\FormFactory |
| 36 | + */ |
| 37 | + private $customerFormFactory; |
| 38 | + |
| 39 | + /** |
| 40 | + * Address helper |
| 41 | + * |
| 42 | + * @var \Magento\Customer\Helper\Address |
| 43 | + */ |
| 44 | + private $addressHelper; |
| 45 | + |
| 46 | + /** |
| 47 | + * Directory helper |
| 48 | + * |
| 49 | + * @var \Magento\Directory\Helper\Data |
| 50 | + */ |
| 51 | + private $directoryHelper; |
| 52 | + |
| 53 | + /** |
| 54 | + * Session quote |
| 55 | + * |
| 56 | + * @var \Magento\Backend\Model\Session\Quote |
| 57 | + */ |
| 58 | + private $session; |
| 59 | + |
| 60 | + /** |
| 61 | + * Json encoder |
| 62 | + * |
| 63 | + * @var \Magento\Framework\Serialize\Serializer\Json |
| 64 | + */ |
| 65 | + private $jsonEncoder; |
| 66 | + |
| 67 | + /** |
| 68 | + * Customer address |
| 69 | + * |
| 70 | + * @param \Magento\Customer\Model\ResourceModel\Address\Collection $addressCollection |
| 71 | + * @param \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory |
| 72 | + * @param \Magento\Customer\Helper\Address $addressHelper |
| 73 | + * @param \Magento\Directory\Helper\Data $directoryHelper |
| 74 | + * @param \Magento\Backend\Model\Session\Quote $session |
| 75 | + * @param \Magento\Framework\Serialize\Serializer\Json $jsonEncoder |
| 76 | + */ |
| 77 | + public function __construct( |
| 78 | + \Magento\Customer\Model\ResourceModel\Address\Collection $addressCollection, |
| 79 | + \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory, |
| 80 | + \Magento\Customer\Helper\Address $addressHelper, |
| 81 | + \Magento\Directory\Helper\Data $directoryHelper, |
| 82 | + \Magento\Backend\Model\Session\Quote $session, |
| 83 | + \Magento\Framework\Serialize\Serializer\Json $jsonEncoder |
| 84 | + ) { |
| 85 | + $this->addressCollection = $addressCollection; |
| 86 | + $this->customerFormFactory = $customerFormFactory; |
| 87 | + $this->addressHelper = $addressHelper; |
| 88 | + $this->directoryHelper = $directoryHelper; |
| 89 | + $this->session = $session; |
| 90 | + $this->jsonEncoder = $jsonEncoder; |
| 91 | + parent::__construct(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Retrieve customer address array. |
| 96 | + * |
| 97 | + * @param int $customerId |
| 98 | + * |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function getAddresses(int $customerId): array |
| 102 | + { |
| 103 | + if ($customerId) { |
| 104 | + if ($this->addressArray === null) { |
| 105 | + $addressCollection = $this->addressCollection->setCustomerFilter([$customerId]); |
| 106 | + $this->addressArray = $addressCollection->toArray(); |
| 107 | + } |
| 108 | + return $this->addressArray; |
| 109 | + } |
| 110 | + return []; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Return customer address array as JSON |
| 115 | + * |
| 116 | + * @param int $customerId |
| 117 | + * |
| 118 | + * @return string |
| 119 | + */ |
| 120 | + public function getAddressesJson(int $customerId): string |
| 121 | + { |
| 122 | + $data = $this->getEmptyAddressForm(); |
| 123 | + foreach ($this->getAddresses($customerId) as $addressId => $address) { |
| 124 | + $addressForm = $this->customerFormFactory->create( |
| 125 | + 'customer_address', |
| 126 | + 'adminhtml_customer_address', |
| 127 | + $address |
| 128 | + ); |
| 129 | + $data[$addressId] = $addressForm->outputData( |
| 130 | + \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + return $this->jsonEncoder->serialize($data); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Represent customer address in 'online' format. |
| 139 | + * |
| 140 | + * @param array $address |
| 141 | + * @return string |
| 142 | + */ |
| 143 | + public function getAddressAsString(array $address): string |
| 144 | + { |
| 145 | + $formatTypeRenderer = $this->addressHelper->getFormatTypeRenderer('oneline'); |
| 146 | + $result = ''; |
| 147 | + if ($formatTypeRenderer) { |
| 148 | + $result = $formatTypeRenderer->renderArray($address); |
| 149 | + } |
| 150 | + |
| 151 | + return $result; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Return empty address address form |
| 156 | + * |
| 157 | + * @return array |
| 158 | + */ |
| 159 | + private function getEmptyAddressForm(): array |
| 160 | + { |
| 161 | + $defaultCountryId = $this->directoryHelper->getDefaultCountry($this->session->getStore()); |
| 162 | + $emptyAddressForm = $this->customerFormFactory->create( |
| 163 | + 'customer_address', |
| 164 | + 'adminhtml_customer_address', |
| 165 | + [\Magento\Customer\Api\Data\AddressInterface::COUNTRY_ID => $defaultCountryId] |
| 166 | + ); |
| 167 | + |
| 168 | + return [0 => $emptyAddressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON)]; |
| 169 | + } |
| 170 | +} |
0 commit comments