diff --git a/application/models/bestitamazonpay4oxidaddressutil.php b/application/models/bestitamazonpay4oxidaddressutil.php index 72bffed..b2844e4 100644 --- a/application/models/bestitamazonpay4oxidaddressutil.php +++ b/application/models/bestitamazonpay4oxidaddressutil.php @@ -26,6 +26,27 @@ private function _cleanPatternSearchResult(array $matches) return $requiredValues; } + /** + * Saves the street + house number in the street field and remove the company field, if number is saved in company. + * + * Amazon had a short period, in which their address form was wrong so the street number must be handled specially. + * + * @param array $possibleStreetLines + * + * @return array Return the changed to original street lines. + */ + private function _fixBrokenHouseNumberDataIfNeeded(array $possibleStreetLines) + { + if ($this->_lineContainsJustAHouseNumber($possibleStreetLines['usual company'])) { + $possibleStreetLines['usual street'] = $possibleStreetLines['usual street'] . ' ' . + $possibleStreetLines['usual company']; + + $possibleStreetLines['usual company'] = ''; + } + + return $possibleStreetLines; + } + /** * Checks the address lines and returns the matching steet and company information. * @@ -72,6 +93,8 @@ private function _getPossibleStreetLinesInTheConfiguredOrder(stdClass $amazonDat if (isset($aMap[$amazonData->CountryCode])) { // Usually line 2 is the street but if line 2 empty, line 1 becomes the street, so move line 2 to the top. $possibleStreetLines = array_reverse($possibleStreetLines); + + $possibleStreetLines = $this->_fixBrokenHouseNumberDataIfNeeded($possibleStreetLines); } // Line 3 is the company or additional company infos, everytime! But if the other lines are empty, it can be @@ -151,6 +174,18 @@ protected function _isStreetParsingLongerThanNumber(array $followingNumberMatche return strlen((string) @$followingNumberMatches['Name']) > strlen((string) @$followingNumberMatches['Number']); } + /** + * Does it seem that the given address line just contains a house number? + * + * @param string $addressLine + * + * @return bool + */ + private function _lineContainsJustAHouseNumber($addressLine) + { + return (bool) preg_match('/^\s*(?P\d+[\s\w]{0,9})\s*$/', $addressLine); + } + /** * Returns parsed Street name and Street number in array * diff --git a/tests/unit/application/models/bestitAmazonPay4OxidAdressUtilTest.php b/tests/unit/application/models/bestitAmazonPay4OxidAdressUtilTest.php index 5ba67aa..6b34b20 100644 --- a/tests/unit/application/models/bestitAmazonPay4OxidAdressUtilTest.php +++ b/tests/unit/application/models/bestitAmazonPay4OxidAdressUtilTest.php @@ -231,6 +231,210 @@ public function getFullAddressParseAsserts() ), true, false + ), + // Amazon had a short period, in which their address form was wrong + // so the street number must be handled specially. + 'map pseudo code with all filled fields, company on top like in germany, but broken street nr' => array( + array( + 'AddressLine1' => 'Street Line 1a additional', + 'AddressLine2' => ' 50N', + 'AddressLine3' => 'Company Line 3', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Company Line 3', + 'Street' => 'Street Line', + 'StreetNr' => '1a', + 'AddInfo' => 'additional 50N', + ), + true + ), + 'map pseudo code with company and street, company on top like in germany, but broken street nr' => array( + array( + 'AddressLine1' => 'Street Line', + 'AddressLine2' => ' 50N', + 'AddressLine3' => 'Company Line 3', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Company Line 3', + 'Street' => 'Street Line', + 'StreetNr' => '50N', + 'AddInfo' => '', + ), + true + ), + 'map pseudo code with just street, company on top like in germany, but broken street nr' => array( + array( + 'AddressLine1' => 'Street Line', + 'AddressLine2' => ' 50N', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => '', + 'Street' => 'Street Line', + 'StreetNr' => '50N', + 'AddInfo' => '', + ), + true + ), + 'map pseudo code with all filled fields, company on top like in germany' => array( + array( + 'AddressLine1' => 'Company Line 1', + 'AddressLine2' => 'Street Line 2a additional', + 'AddressLine3' => 'Info Line 3', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Company Line 1, Info Line 3', + 'Street' => 'Street Line', + 'StreetNr' => '2a', + 'AddInfo' => 'additional', + ), + true + ), + 'map pseudo code without line 2, company on top like in germany' => array( + array( + 'AddressLine1' => 'Street Line 1a additional', + 'AddressLine3' => 'Info Line 3', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Info Line 3', + 'Street' => 'Street Line', + 'StreetNr' => '1a', + 'AddInfo' => 'additional', + ), + true + ), + 'map pseudo code without line 2 and 3, company on top like in germany' => array( + array( + 'AddressLine1' => 'Street Line 1a', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => '', + 'Street' => 'Street Line', + 'StreetNr' => '1a', + 'AddInfo' => '', + ), + true + ), + 'map pseudo code with all filled fields, normal order' => array( + array( + 'AddressLine1' => 'Street Line 1a additional', + 'AddressLine2' => 'Company Line 2', + 'AddressLine3' => 'Info Line 3', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Company Line 2, Info Line 3', + 'Street' => 'Street Line', + 'StreetNr' => '1a', + 'AddInfo' => 'additional', + ) + ), + 'map pseudo code with all filled fields but no info line, normal order' => array( + array( + 'AddressLine1' => 'Street Line 1a additional', + 'AddressLine2' => 'Company Line 2', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Company Line 2', + 'Street' => 'Street Line', + 'StreetNr' => '1a', + 'AddInfo' => 'additional', + ) + ), + 'map pseudo code with line 1 but no company line, normal order' => array( + array( + 'AddressLine1' => 'Street Line 1a additional', + 'AddressLine2' => '', + 'AddressLine3' => 'Info Line 3', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Info Line 3', + 'Street' => 'Street Line', + 'StreetNr' => '1a', + 'AddInfo' => 'additional', + ) + ), + 'map pseudo code with line 1 but nothing else, normal order' => array( + array( + 'AddressLine1' => 'Street Line 1a additional', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => '', + 'Street' => 'Street Line', + 'StreetNr' => '1a', + 'AddInfo' => 'additional', + ) + ), + 'map pseudo code with all filled fields but line 1, normal order' => array( + array( + 'AddressLine2' => 'Street Line 2b additional', + 'AddressLine3' => 'Company Line 3', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => 'Company Line 3', + 'Street' => 'Street Line', + 'StreetNr' => '2b', + 'AddInfo' => 'additional', + ) + ), + 'map pseudo code with just line 2, normal order' => array( + array( + 'AddressLine2' => 'Street Line 2b', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => '', + 'Street' => 'Street Line', + 'StreetNr' => '2b', + 'AddInfo' => '', + ) + ), + 'map pseudo code with just line 3, normal order' => array( + array( + 'AddressLine2' => 'StreetLine 3c', + 'Name' => 'FName MName LName' + ), + array( + 'LastName' => 'LName', + 'FirstName' => 'FName MName', + 'CompanyName' => '', + 'Street' => 'StreetLine', + 'StreetNr' => '3c', + 'AddInfo' => '', + ) ) ); }