@@ -68,11 +68,9 @@ public function buildForPickupSave(Address $billingAddress, LocationInterface $l
68
68
// Extract street and housenumber from street array
69
69
/** @var string|array $street */
70
70
$ street = $ billingAddress ->getStreet ();
71
- $ streetString = is_array ($ street ) ?
72
- implode (' ' , $ street ) :
73
- $ street ;
74
- $ streetWithoutNumber = preg_match ('/^(.*?)(?=\d)/ ' , $ streetString , $ matches ) ? $ matches [0 ] : $ streetString ;
75
- $ houseNumber = preg_match ('/\d(.*)/ ' , $ streetString , $ matches ) ? $ matches [0 ] : '' ;
71
+
72
+ $ streetWithoutNumber = $ this ->getStreetWithoutHouseNumber ($ billingAddress );
73
+ $ houseNumber = $ this ->getHousenumber ($ billingAddress );
76
74
77
75
// Customer data
78
76
$ request ->setCustomerData (
@@ -123,15 +121,9 @@ public function buildForDeliverySave(
123
121
// Extract street and housenumber from street array
124
122
/** @var string|array $street */
125
123
$ street = $ shippingAddress ->getStreet ();
126
- $ streetString = is_array ($ street ) ?
127
- implode (' ' , $ street ) :
128
- $ street ;
129
- $ streetWithoutNumber = preg_match ('/^(.*?)(?=\d)/ ' , $ streetString , $ matches ) ?
130
- $ matches [0 ] :
131
- $ streetString ;
132
- $ houseNumber = preg_match ('/\d(.*)/ ' , $ streetString , $ matches ) ?
133
- $ matches [0 ] :
134
- '' ;
124
+
125
+ $ streetWithoutNumber = $ this ->getStreetWithoutHouseNumber ($ shippingAddress );
126
+ $ houseNumber = $ this ->getHousenumber ($ shippingAddress );
135
127
136
128
$ request ->setAddress (
137
129
[
@@ -178,23 +170,13 @@ public function buildForLocations(Address $shippingAddress): PostnlRequestInterf
178
170
/** @var string|array $street */
179
171
$ street = $ shippingAddress ->getStreet ();
180
172
181
- $ streetWithoutNumber = '' ;
182
- $ houseNumber = '' ;
183
- if (is_array ($ street ) && count ($ street ) >= 2 ) {
184
- $ street = array_values (array_filter ($ street ));
185
- $ streetWithoutNumber = $ street [0 ] ?? '' ;
186
- $ houseNumber = $ street [1 ] ?? '' ;
187
- }
188
-
189
- if (is_string ($ street )) {
190
- $ streetWithoutNumber = preg_match ('/^(.*?)(?=\d)/ ' , $ street , $ matches ) ? $ matches [0 ] : $ street ;
191
- $ houseNumber = preg_match ('/\d(.*)/ ' , $ street , $ matches ) ? $ matches [0 ] : '' ;
192
- }
173
+ $ streetWithoutNumber = $ this ->getStreetWithoutHouseNumber ($ shippingAddress );
174
+ $ houseNumber = $ this ->getHousenumber ($ shippingAddress );
193
175
194
176
// Location address data
195
177
$ request ->setAddress ([
196
178
'country ' => $ shippingAddress ->getCountryId (),
197
- 'street ' => trim (( string ) $ streetWithoutNumber) ,
179
+ 'street ' => $ streetWithoutNumber ,
198
180
'postcode ' => $ shippingAddress ->getPostcode (),
199
181
'housenumber ' => $ houseNumber ,
200
182
'firstname ' => $ shippingAddress ->getFirstname (),
@@ -223,4 +205,56 @@ public function buildforTimeframes(Address $address): PostnlRequestInterface
223
205
// The request for timeframes is identical to locations.
224
206
return $ this ->buildForLocations ($ address );
225
207
}
208
+
209
+ private function getStreetWithoutHouseNumber (Address $ address ): string
210
+ {
211
+ $ street = $ address ->getStreet ();
212
+ if (!is_array ($ street )) {
213
+ throw new LocalizedException (__ ('Street should be an array ' ));
214
+ }
215
+
216
+ // Get the first street, containing the street name
217
+ $ firstStreetItem = reset ($ street );
218
+
219
+ // Check if we're dealing with a single line street. If that's the case we'll extract the street using regex.
220
+ if (count ($ street ) === 1 ) {
221
+ preg_match ('/^(?:\b\d\w*\b\s*)?(\b[a-zA-Z]\w*\b\s*)*/ ' , $ firstStreetItem , $ matches );
222
+
223
+ // Get the first street, containing the street name
224
+ return trim ($ matches [0 ]);
225
+ }
226
+
227
+ // If we're dealing with a multi line street, just return the first line.
228
+ return trim ($ firstStreetItem );
229
+ }
230
+
231
+ private function getHousenumber (Address $ address ): string
232
+ {
233
+ $ street = $ address ->getStreet ();
234
+ if (!$ street ) {
235
+ throw new LocalizedException (__ ('Street should be an array ' ));
236
+ }
237
+
238
+ // Trim spaces from all lines
239
+ $ trimmedStreet = array_map (
240
+ static function ($ line ) {
241
+ return trim ($ line );
242
+ },
243
+ $ street
244
+ );
245
+
246
+ // Check if we're dealing with a single line street. If that's the case we'll extract the street using regex.
247
+ $ streetWithNumberPattern = '/\s(\d+[A-Za-z\s-]*)/ ' ;
248
+ $ firstStreetItem = reset ($ street );
249
+ if (count ($ street ) === 1 && preg_match ($ streetWithNumberPattern , $ firstStreetItem , $ matches )) {
250
+ // Get the first street, containing the street name
251
+ return trim ($ matches [0 ]);
252
+ }
253
+
254
+ // Remove first item
255
+ array_shift ($ trimmedStreet );
256
+
257
+ // And return as string
258
+ return trim (implode (' ' , $ trimmedStreet ));
259
+ }
226
260
}
0 commit comments