Skip to content

Commit

Permalink
Merge pull request #116 from bestit/features/make-street-handling-easier
Browse files Browse the repository at this point in the history
OXAP-149 Added street parsing without the config of a country iso code
  • Loading branch information
b3nl authored Oct 10, 2019
2 parents 928e314 + b29f5ce commit 3b15645
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 115 deletions.
160 changes: 147 additions & 13 deletions application/models/bestitamazonpay4oxidaddressutil.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,127 @@
*/
class bestitAmazonPay4OxidAddressUtil extends bestitAmazonPay4OxidContainer
{
/**
* Return the parsing which returns more pattern hits and contains a longer street name.
*
* @param array $followingNumberMatches
* @param array $leadingNumberMatches
*
* @return array
*/
protected function _handleParsingForInconclusiveMatches(array $followingNumberMatches, array $leadingNumberMatches)
{
$leadingNumberMatchesCount = count($leadingNumberMatches);
$followingNumberMatchesCount = count($followingNumberMatches);
$relevantParsing = array();

if ($leadingNumberMatchesCount === $followingNumberMatchesCount) {
if ($this->_isStreetParsingLongerThanNumber($followingNumberMatches)) {
$relevantParsing = $followingNumberMatches;
}
} else {
$isFollowingNumberMatchMoreRelevant = $this->_isFollowingNumberMatchMoreRelevant(
$followingNumberMatchesCount,
$leadingNumberMatchesCount
);

$relevantParsing = $isFollowingNumberMatchMoreRelevant ? $followingNumberMatches : $leadingNumberMatches;
}

return $relevantParsing;
}

/**
* If both parsings match exactly than the parsing was inconclusive.
*
* @param array|bool $followingNumberMatches
* @param array|bool $leadingNumberMatches
*
* @return bool
*/
protected function _isAdressLineParsingInconclusive($followingNumberMatches, $leadingNumberMatches)
{
return $leadingNumberMatches && $followingNumberMatches;
}

/**
* Can a following number be found?
*
* @param int $followingNumberMatchesCount
* @param int $leadingNumberMatchesCount
*
* @return bool
*/
protected function _isFollowingNumberMatchMoreRelevant($followingNumberMatchesCount, $leadingNumberMatchesCount)
{
return $followingNumberMatchesCount > $leadingNumberMatchesCount;
}

/**
* A longer street name than the number suggests, that the parsing was correct.
*
* @param array $followingNumberMatches
*
* @return bool
*/
protected function _isStreetParsingLongerThanNumber(array $followingNumberMatches)
{
return strlen((string) @$followingNumberMatches['Name']) > strlen((string) @$followingNumberMatches['Number']);
}

/**
* Returns parsed Street name and Street number in array
*
* @param string $sString Full address
* @param string $sIsoCountryCode ISO2 code of country of address
* @param string $addressLine
* @param string $iso2CountryCode
*
* @return string
* @return array
*/
protected function _parseSingleAddress($sString, $sIsoCountryCode = null)
protected function _parseSingleAddress($addressLine, $iso2CountryCode)
{
// Array of iso2 codes of countries that have address format <street_no> <street>
$aStreetNoStreetCountries = $this->getConfig()->getConfigParam('aAmazonStreetNoStreetCountries');
$leadingNumberMatches = $this->_searchForLeadingNumberInAddressLine($addressLine);
$followingNumberMatches = $this->_searchForFollowingNumberInAddressLine($addressLine);

$relevantParsing = $leadingNumberMatches;

if (in_array($sIsoCountryCode, $aStreetNoStreetCountries)) {
// matches streetname/streetnumber like "streetnumber streetname"
preg_match('/\s*(?P<Number>\d[^\s]*)*\s*(?P<Name>[^\d]*[^\d\s])\s*(?P<AddInfo>.*)/', $sString, $aResult);
if ($this->_isAdressLineParsingInconclusive($followingNumberMatches, $leadingNumberMatches)) {
$this->getLogger()->debug(
'The address parsing was not conclusive.',
array(
'countyCode' => $iso2CountryCode,
'originalAddressLine' => $addressLine
)
);

$relevantParsing = $this->_handleParsingForInconclusiveMatches(
$followingNumberMatches,
$leadingNumberMatches
);
} else {
// default: matches streetname/streetnumber like "streetname streetnumber"
preg_match('/\s*(?P<Name>[^\d]*[^\d\s])\s*((?P<Number>\d[^\s]*)\s*(?P<AddInfo>.*))*/', $sString, $aResult);
$this->getLogger()->debug(
'The address parsing was conclusive.',
array(
'countyCode' => $iso2CountryCode,
'originalAddressLine' => $addressLine
)
);

if ($followingNumberMatches) {
$relevantParsing = $followingNumberMatches;
}
}
// else is hidden thru the default value!

return $aResult;
$this->getLogger()->info(
'Parsed the given single address line to a value array.',
array(
'countyCode' => $iso2CountryCode,
'originalAddressLine' => $addressLine,
'parsedAddressLine' => $relevantParsing
)
);

return $relevantParsing;
}

/**
Expand All @@ -50,10 +149,13 @@ protected function _parseAddressFields($oAmazonData, array &$aResult)
$aReverseOrderCountries = $this->getConfig()->getConfigParam('aAmazonReverseOrderCountries');

$aMap = array_flip($aReverseOrderCountries);
$aCheckOrder = isset($aMap[$oAmazonData->CountryCode]) === true ? array (2, 1) : array(1, 2);
$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 !== '') {
Expand Down Expand Up @@ -82,6 +184,38 @@ protected function _parseAddressFields($oAmazonData, array &$aResult)
);
}

/**
* 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.
*/
protected function _searchForFollowingNumberInAddressLine($addressLine)
{
return preg_match(
'/\s*(?P<Name>[^\d]*[^\d\s])\s*((?P<Number>\d[^\s]*)\s*(?P<AddInfo>.*))*/',
$addressLine,
$matches
) ? $matches : false;
}

/**
* Matches a pattern with a leading 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.
*/
protected function _searchForLeadingNumberInAddressLine($addressLine)
{
return preg_match(
'/\s*(?P<Number>\d[^\s]*)*\s*(?P<Name>[^\d]*[^\d\s])\s*(?P<AddInfo>.*)/',
$addressLine,
$matches
) ? $matches : false;
}

/**
* Returns Parsed address from Amazon by specific rules
*
Expand Down
1 change: 0 additions & 1 deletion application/views/admin/de/bestitamazonpay4oxid_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
'SHOP_MODULE_blShowAmazonPayButtonAtDetails' => 'Amazon Pay-Button auf Produktdetailseite anzeigen',
'SHOP_MODULE_blShowAmazonPayButtonAtCartPopup' => 'Amazon Pay-Button im Warenkorb Popup anzeigen',
'SHOP_MODULE_aAmazonReverseOrderCountries' => 'ISO2 Code der Länder, bei denen die AddressLineX Rückgaben von Amazon vertauscht sind (AddressLine1 == Firma, AddressLine2 == Straße)',
'SHOP_MODULE_aAmazonStreetNoStreetCountries' => 'ISO2 Code der Länder, die Straße und Hausnummer vertauscht haben',

'SHOP_MODULE_GROUP_bestitAmazonPay4OxidLanguages' => 'Sprach Einstellungen',
'SHOP_MODULE_aAmazonLanguages' => "Sprach Einstellungen ('Oxid Sprachk&uuml;rzel' => 'Amazon Sprachwert')",
Expand Down
1 change: 0 additions & 1 deletion application/views/admin/en/bestitamazonpay4oxid_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
'SHOP_MODULE_blShowAmazonPayButtonAtDetails' => 'Show Amazon Pay button on the details page',
'SHOP_MODULE_blShowAmazonPayButtonAtCartPopup' => 'Show Amazon Pay button on the cart popup',
'SHOP_MODULE_aAmazonReverseOrderCountries' => 'ISO2 code of the countries where the AddressLineX returned from Amazon are reversed (AddressLine1 == company, AddressLine2 == street)',
'SHOP_MODULE_aAmazonStreetNoStreetCountries' => 'ISO2 Code of the countries that have reversed street and house numbers',

'SHOP_MODULE_GROUP_bestitAmazonPay4OxidLanguages' => 'Language Settings',
'SHOP_MODULE_aAmazonLanguages' => "Language mapping ('Oxid language abbreviation' => 'Amazon language value')",
Expand Down
7 changes: 0 additions & 7 deletions metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,6 @@
'value' => array('DE', 'AT', 'FR'),
'position' => 11
),
array(
'group' => 'bestitAmazonPay4OxidConfiguration',
'name' => 'aAmazonStreetNoStreetCountries',
'type' => 'arr',
'value' => array('FR', 'GB'),
'position' => 12
),
),
'events' => array(
'onActivate' => 'bestitAmazonPay4Oxid_init::onActivate',
Expand Down
Loading

0 comments on commit 3b15645

Please sign in to comment.