66use Flekto \Postcode \Helper \StoreConfigHelper ;
77use Flekto \Postcode \Service \Exception \NotFoundException ;
88use Flekto \Postcode \Service \PostcodeApiClient ;
9+ use Magento \Customer \Helper \Address as AddressHelper ;
910use Magento \Developer \Helper \Data ;
1011use Magento \Directory \Model \RegionFactory ;
1112use Magento \Framework \App \Helper \AbstractHelper ;
@@ -34,6 +35,7 @@ class ApiClientHelper extends AbstractHelper
3435 protected $ _storeConfigHelper ;
3536 protected $ _productMetadata ;
3637 protected $ _regionFactory ;
38+ protected $ _addressHelper ;
3739
3840 /**
3941 * __construct function.
@@ -49,6 +51,7 @@ class ApiClientHelper extends AbstractHelper
4951 * @param StoreConfigHelper $storeConfigHelper
5052 * @param ProductMetadataInterface $productMetadata
5153 * @param RegionFactory $regionFactory
54+ * @param AddressHelper $addressHelper
5255 * @return void
5356 */
5457 public function __construct (
@@ -61,7 +64,8 @@ public function __construct(
6164 LocaleResolver $ localeResolver ,
6265 StoreConfigHelper $ storeConfigHelper ,
6366 ProductMetadataInterface $ productMetadata ,
64- RegionFactory $ regionFactory
67+ RegionFactory $ regionFactory ,
68+ AddressHelper $ addressHelper
6569 ) {
6670 $ this ->_moduleList = $ moduleList ;
6771 $ this ->_developerHelper = $ developerHelper ;
@@ -72,6 +76,7 @@ public function __construct(
7276 $ this ->_storeConfigHelper = $ storeConfigHelper ;
7377 $ this ->_productMetadata = $ productMetadata ;
7478 $ this ->_regionFactory = $ regionFactory ;
79+ $ this ->_addressHelper = $ addressHelper ;
7580 parent ::__construct ($ context );
7681 }
7782
@@ -129,6 +134,7 @@ public function getAddressDetails(string $context): array
129134 $ sessionId = $ this ->_getSessionId ();
130135 $ response = $ client ->internationalGetDetails ($ context , $ sessionId );
131136 $ response ['region ' ] = $ this ->_getRegionFromDetails ($ response );
137+ $ response ['streetLines ' ] = $ this ->_getStreetLines ($ response );
132138 $ response = $ this ->_prepareResponse ($ response , $ client );
133139
134140 return $ response ;
@@ -203,6 +209,54 @@ protected function _getRegionByName(string $name, string $countryIso2): array
203209 return ['id ' => $ id ?? null , 'name ' => $ name ?? null ];
204210 }
205211
212+ /**
213+ * Get street lines from an address details response.
214+ *
215+ * The amount of lines is limited by the configured number of lines in a street address.
216+ *
217+ * @param array $addressDetails
218+ * @return array - Street lines formatted according to country and config.
219+ */
220+ protected function _getStreetLines (array $ addressDetails ): array
221+ {
222+ $ address = $ addressDetails ['address ' ];
223+ $ countryIso2 = $ addressDetails ['country ' ]['iso2Code ' ];
224+ $ lastLineIndex = $ this ->_addressHelper ->getStreetLines () - 1 ;
225+
226+ if ($ this ->_storeConfigHelper ->isSetFlag ('split_street_values ' )) {
227+ // Assume fields are fixed street parts, independent of country.
228+ $ parts = [
229+ $ address ['street ' ],
230+ $ address ['buildingNumber ' ] ?? '' ,
231+ $ address ['buildingNumberAddition ' ] ?? '' ,
232+ ];
233+ $ lines = array_slice ($ parts , 0 , $ lastLineIndex );
234+ $ lines [] = implode (' ' , array_slice ($ parts , $ lastLineIndex ));
235+ } elseif ($ countryIso2 === 'LU ' ) {
236+ $ lines = [$ address ['building ' ] . ', ' . $ address ['street ' ]];
237+ } elseif ($ countryIso2 === 'FR ' ) {
238+ $ lines = [trim ($ address ['building ' ] . ' ' . $ address ['street ' ])];
239+ } elseif ($ countryIso2 === 'GB ' ) {
240+ $ building = $ addressDetails ['details ' ]['gbrBuilding ' ];
241+ if ($ address ['street ' ] === '' ) {
242+ $ separator = '' ;
243+ } elseif ($ building ['number ' ] === null && $ building ['addition ' ] === null ) {
244+ $ separator = ', ' ;
245+ } else {
246+ $ separator = ' ' ;
247+ }
248+
249+ // Support multiple lines in British address.
250+ $ parts = explode (', ' , $ address ['building ' ] . $ separator . $ address ['street ' ]);
251+ $ lines = array_slice ($ parts , 0 , $ lastLineIndex );
252+ $ lines [] = implode (', ' , array_slice ($ parts , $ lastLineIndex ));
253+ } else {
254+ $ lines = [trim ($ address ['street ' ] . ' ' . $ address ['building ' ])];
255+ }
256+
257+ return $ lines ;
258+ }
259+
206260 /**
207261 * Get session identifier.
208262 *
@@ -445,13 +499,13 @@ private function _getDebugInfo(): array
445499 'configuration ' => [
446500 'key ' => $ credentials ['key ' ],
447501 'secret ' => substr_replace ($ credentials ['secret ' ], '*** ' , 3 , -3 ),
448- 'debug ' => $ this ->_storeConfigHelper ->getValue (StoreConfigHelper:: PATH [ 'api_debug ' ] ),
502+ 'debug ' => $ this ->_storeConfigHelper ->getValue ('api_debug ' ),
449503 ],
450504 'modules ' => $ this ->_getMagentoModules (),
451505 ];
452506
453507 // Module version
454- $ debug ['moduleVersion ' ] = $ this ->_storeConfigHelper ->getValue (StoreConfigHelper:: PATH [ 'module_version ' ] );
508+ $ debug ['moduleVersion ' ] = $ this ->_storeConfigHelper ->getValue ('module_version ' );
455509
456510 // Magento version
457511 $ version = $ this ->_productMetadata ->getVersion ();
0 commit comments