@@ -16,6 +16,12 @@ class Address extends Model {
1616 */
1717 public $ lastName ;
1818
19+ /**
20+ * The 'extra' name, used for pickup points
21+ * @var string
22+ */
23+ public $ extraName ;
24+
1925 /**
2026 * Postal code
2127 * @var string
@@ -55,21 +61,32 @@ class Address extends Model {
5561 /**
5662 * Funky code to get the housenumber and addition out of a street
5763 * The mashape API (used for DHL pickup points) only returns address data
58- * @param type $street
59- * @return Address
64+ * @param srting $street Can be address (with housenumber)
65+ * @param string $postalCode
66+ * @param string $city
67+ * @param integer $houseNumber Optional
68+ * @param string $houseNumberAddition Optional
69+ * @return \Afosto\ActiveAnts\Address
6070 */
61- public static function getAddressFromStreet ($ street ) {
71+ public static function formatAddress ($ street, $ postalCode , $ city , $ houseNumber = null , $ houseNumberAddition = null ) {
6272 $ address = new Address ();
63- $ pattern = '#^([a-z0-9 [:punct:] \']*) ([0-9]{1,5})([a-z0-9 \-/]{0,})$#i ' ;
64- preg_match ($ pattern , str_replace (' - ' , '- ' , $ street ), $ aMatch );
65-
66- $ address ->street = $ aMatch [1 ];
67- $ address ->houseNumber = preg_replace ("/[^A-Za-z0-9 ]/ " , '' , $ aMatch [2 ]);
68-
69- if (isset ($ aMatch [3 ])) {
70- $ address ->houseNumberAddition = preg_replace ("/[^A-Za-z0-9 ]/ " , '' , $ aMatch [3 ]);
73+ if (is_null ($ houseNumber )) {
74+ //If housenumber was not provided, try to retreive it from the street (address)
75+ $ pattern = '#^([a-z0-9 [:punct:] \']*) ([0-9]{1,5})([a-z0-9 \-/]{0,})$#i ' ;
76+ preg_match ($ pattern , str_replace (' - ' , '- ' , $ street ), $ aMatch );
77+ $ address ->street = $ aMatch [1 ];
78+ $ address ->houseNumber = preg_replace ("/[^A-Za-z0-9 ]/ " , '' , $ aMatch [2 ]);
79+ if (isset ($ aMatch [3 ])) {
80+ $ address ->houseNumberAddition = preg_replace ("/[^A-Za-z0-9 ]/ " , '' , $ aMatch [3 ]);
81+ }
82+ } else {
83+ $ address ->street = $ street ;
84+ $ address ->houseNumber = $ houseNumber ;
85+ $ address ->houseNumberAddition = $ houseNumberAddition ;
7186 }
72-
87+ $ address ->postalCode = $ postalCode ;
88+ $ address ->cityName = $ city ;
89+
7390 return $ address ;
7491 }
7592
@@ -149,4 +166,23 @@ public function getAddress($type) {
149166 return $ address ;
150167 }
151168
169+ /**
170+ * Sets the 'extra' name
171+ */
172+ public function setExtraName () {
173+ $ this ->extraName = 'T.a.v. ' . $ this ->firstName . ' ' . $ this ->lastName .
174+ ' ( ' . $ this ->getFullHouseNumber () . ') ' ;
175+ }
176+
177+ /**
178+ * Return the formatted housenumber
179+ * @return string
180+ */
181+ public function getFullHouseNumber () {
182+ if (is_null ($ this ->houseNumberAddition ) || trim ($ this ->houseNumberAddition ) == '' ) {
183+ return $ this ->houseNumber ;
184+ }
185+ return $ this ->houseNumber . ' ' . $ this ->houseNumberAddition ;
186+ }
187+
152188}
0 commit comments