Skip to content

Commit

Permalink
Merge pull request #117 from bestit/feature/OXAP-292-refactor-company…
Browse files Browse the repository at this point in the history
…-handling

Feature/oxap 292 refactor company handling
  • Loading branch information
b3nl authored Oct 10, 2019
2 parents 3b15645 + 301b6e6 commit 65ab614
Show file tree
Hide file tree
Showing 2 changed files with 594 additions and 194 deletions.
176 changes: 129 additions & 47 deletions application/models/bestitamazonpay4oxidaddressutil.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,105 @@
*/
class bestitAmazonPay4OxidAddressUtil extends bestitAmazonPay4OxidContainer
{
/**
* Filters the pattern result and adds default values for the required fields.
*
* @param array $matches
*
* @return array
*/
private function _cleanPatternSearchResult(array $matches)
{
$requiredFields = array('Street', 'StreetNr', 'AddInfo');
$requiredValues = array();

foreach ($requiredFields as $requiredField) {
$requiredValues[$requiredField] = isset($matches[$requiredField]) ? $matches[$requiredField] : '';
}

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.
*
* @param stdClass $amazonData
*
* @return array The first value is the company and second value is the street line.
*/
protected function _getCompanyAndStreetLineFromAmazonData(stdClass $amazonData)
{
$possibleStreetLines = $this->_getPossibleStreetLinesInTheConfiguredOrder($amazonData);

do {
// Search the street.
$street = (string) array_shift($possibleStreetLines);
} while (!$street && $possibleStreetLines);

// Use the possible remainder as the company.
$company = ($possibleCompanyLines = array_filter($possibleStreetLines))
? implode(', ', $possibleCompanyLines)
: '';

return array($company, $street);
}

/**
* Returns the possible streets fields from the amazon data relative to the configured order for the spec. country
*
* @param stdClass $amazonData
*
* @return array
*/
private function _getPossibleStreetLinesInTheConfiguredOrder(stdClass $amazonData)
{
$possibleStreetLines = array(
// If line 1 is filled it becomes the street usually and the second line the company.
// If line 1 is empty, the street falls back to line 2.
'usual street' => is_string($amazonData->AddressLine1) ? trim($amazonData->AddressLine1) : '',
'usual company' => is_string($amazonData->AddressLine2) ? trim($amazonData->AddressLine2) : '',
);

$countriesWithCompanyOnTop = $this->getConfig()->getConfigParam('aAmazonReverseOrderCountries');
$countryIsosAsKeys = array_flip($countriesWithCompanyOnTop);

if (isset($countryIsosAsKeys[$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
// become the fallback street as well.
$possibleStreetLines['definitive company info'] = is_string($amazonData->AddressLine3)
? trim($amazonData->AddressLine3)
: '';

return $possibleStreetLines;
}

/**
* Return the parsing which returns more pattern hits and contains a longer street name.
*
Expand Down Expand Up @@ -75,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<StreetNr>\d+[\s\w]{0,9})\s*$/', $addressLine);
}

/**
* Returns parsed Street name and Street number in array
*
Expand Down Expand Up @@ -118,6 +229,8 @@ protected function _parseSingleAddress($addressLine, $iso2CountryCode)
}
// else is hidden thru the default value!

$relevantParsing = $this->_cleanPatternSearchResult($relevantParsing);

$this->getLogger()->info(
'Parsed the given single address line to a value array.',
array(
Expand All @@ -133,68 +246,39 @@ protected function _parseSingleAddress($addressLine, $iso2CountryCode)
/**
* Parses the amazon address fields.
*
* @param \stdClass $oAmazonData
* @param array $aResult
* @param stdClass $oAmazonData
*
* @return array The parsed address result.
*/
protected function _parseAddressFields($oAmazonData, array &$aResult)
protected function _parseAddressFields($oAmazonData)
{
// Cleanup address fields and store them to an array
$aAmazonAddresses = array(
1 => is_string($oAmazonData->AddressLine1) ? trim($oAmazonData->AddressLine1) : '',
2 => is_string($oAmazonData->AddressLine2) ? trim($oAmazonData->AddressLine2) : '',
3 => is_string($oAmazonData->AddressLine3) ? trim($oAmazonData->AddressLine3) : ''
);
list($sCompany, $sStreet) = $this->_getCompanyAndStreetLineFromAmazonData($oAmazonData);

// Array of iso2 codes of countries that have another addressline order
$aReverseOrderCountries = $this->getConfig()->getConfigParam('aAmazonReverseOrderCountries');

$aMap = array_flip($aReverseOrderCountries);
$aCheckOrder = isset($aMap[$oAmazonData->CountryCode]) === true ? array(2, 1) : array(1, 2);
$sStreet = '';
$sCompany = '';

// TODO: Fix it with OXAP-292. Understanding this is not mentally-easy.
// The break is used in "reversed order" (company is filled after the street),
// this feels like "pfeil durch die brust ins auge."
foreach ($aCheckOrder as $iCheck) {
if ($aAmazonAddresses[$iCheck] !== '') {
if ($sStreet !== '') {
$sCompany = $aAmazonAddresses[$iCheck];
break;
}

$sStreet = $aAmazonAddresses[$iCheck];
}
}
$result = array('AddInfo' => '', 'CompanyName' => $sCompany, 'Street' => '', 'StreetNr' => '');

if ($aAmazonAddresses[3] !== '') {
$sCompany = ($sCompany === '') ? $aAmazonAddresses[3] : "{$sCompany}, {$aAmazonAddresses[3]}";
if ($sStreet) {
$result = array_merge($result, $this->_parseSingleAddress($sStreet, $oAmazonData->CountryCode));
}

$aResult['CompanyName'] = $sCompany;

$aAddress = $this->_parseSingleAddress($sStreet, $oAmazonData->CountryCode);
$aResult['Street'] = isset($aAddress['Name']) === true ? $aAddress['Name'] : '';
$aResult['StreetNr'] = isset($aAddress['Number']) === true ? $aAddress['Number'] : '';
$aResult['AddInfo'] = isset($aAddress['AddInfo']) === true ? $aAddress['AddInfo'] : '';

$this->getLogger()->debug(
'Amazon address parsed',
array('result' => $aResult, 'amazonAddress' => $aAmazonAddresses)
array('result' => $result, 'amazonAddress' => $oAmazonData)
);

return $result;
}

/**
* Matches a pattern with a following possible number (as a string) against the given address line.
*
* @param string $addressLine
*
* @return bool|array Contains an array with "Number", "Name", "AddInfo" Key or false on no match.
* @return bool|array Contains an array with "Street", "StreetNr", "AddInfo" Key or false on no match.
*/
protected function _searchForFollowingNumberInAddressLine($addressLine)
{
return preg_match(
'/\s*(?P<Name>[^\d]*[^\d\s])\s*((?P<Number>\d[^\s]*)\s*(?P<AddInfo>.*))*/',
'/\s*(?P<Street>[^\d]*[^\d\s])\s*((?P<StreetNr>\d[^\s]*)\s*(?P<AddInfo>.*))*/',
$addressLine,
$matches
) ? $matches : false;
Expand All @@ -205,12 +289,12 @@ protected function _searchForFollowingNumberInAddressLine($addressLine)
*
* @param string $addressLine
*
* @return bool|array Contains an array with "Number", "Name", "AddInfo" Key or false on no match.
* @return bool|array Contains an array with "StreetNr", "Street", "AddInfo" Key or false on no match.
*/
protected function _searchForLeadingNumberInAddressLine($addressLine)
{
return preg_match(
'/\s*(?P<Number>\d[^\s]*)*\s*(?P<Name>[^\d]*[^\d\s])\s*(?P<AddInfo>.*)/',
'/\s*(?P<StreetNr>\d[^\s]*)*\s*(?P<Street>[^\d]*[^\d\s])\s*(?P<AddInfo>.*)/',
$addressLine,
$matches
) ? $matches : false;
Expand Down Expand Up @@ -245,11 +329,9 @@ public function parseAmazonAddress($oAmazonData)
FROM {$sTable}
WHERE OXISOALPHA2 = ".$this->getDatabase()->quote($oAmazonData->CountryCode);

//Country ID
$aResult['CountryId'] = $this->getDatabase()->getOne($sSql);

//Parsing address
$this->_parseAddressFields($oAmazonData, $aResult);
$aResult = array_merge($aResult, $this->_parseAddressFields($oAmazonData));

//If shop runs in non UTF-8 mode encode values to ANSI
if ($this->getConfig()->isUtf() === false) {
Expand Down
Loading

0 comments on commit 65ab614

Please sign in to comment.