customerFormFactory = $customerFormFactory; $this->addressFormatHelper = $addressFormatHelper; $this->directoryHelper = $directoryHelper; $this->session = $session; $this->jsonEncoder = $jsonEncoder; } /** * Return customer address array as JSON * * @param array $addressArray * * @return string */ public function getAddressesJson(array $addressArray): string { $data = $this->getEmptyAddressForm(); foreach ($addressArray as $addressId => $address) { $addressForm = $this->customerFormFactory->create( 'customer_address', 'adminhtml_customer_address', $address ); $data[$addressId] = $addressForm->outputData( \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON ); } return $this->jsonEncoder->serialize($data); } /** * Represent customer address in 'online' format. * * @param array $address * @return string */ public function getAddressAsString(array $address): string { $formatTypeRenderer = $this->addressFormatHelper->getFormatTypeRenderer('oneline'); $result = ''; if ($formatTypeRenderer) { $result = $formatTypeRenderer->renderArray($address); } return $result; } /** * Return empty address address form * * @return array */ private function getEmptyAddressForm(): array { $defaultCountryId = $this->directoryHelper->getDefaultCountry($this->session->getStore()); $emptyAddressForm = $this->customerFormFactory->create( 'customer_address', 'adminhtml_customer_address', [\Magento\Customer\Api\Data\AddressInterface::COUNTRY_ID => $defaultCountryId] ); return [0 => $emptyAddressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON)]; } }