Skip to content

Commit

Permalink
fix(consignment): replace part of box separator instead of fullstreet (
Browse files Browse the repository at this point in the history
…#441)

* fix(collection): allow array in toArray

* fix: replace box separator instead of full street

* fix: regex

* fix: regex

* fix: regex

* fix: regex

* fix: regex

* fix: codacy error
  • Loading branch information
Mark-Ernst authored Oct 26, 2023
1 parent 07756e7 commit a2f1e29
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
36 changes: 29 additions & 7 deletions src/Helper/SplitStreet.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
class SplitStreet
{
const BOX_NL = 'bus';
const BOX_SEPARATOR = ['boîte', 'box', 'bte', 'Bus'];
const BOX_SEPARATOR_BY_REGEX = ['\/','-', 'B'];
const BOX_BTE = 'bte';
const BOX_FR = 'boîte';
const BOX_EN = 'box';
const BOX_DE = 'Bus';
const BOX_SEPARATOR = [self::BOX_FR, self::BOX_EN, self::BOX_BTE, self::BOX_DE];
const BOX_SLASH = '\/';
const BOX_DASH = '-';
const BOX_B = 'B';
const BOX_SEPARATOR_BY_REGEX = [self::BOX_SLASH, self::BOX_DASH, self::BOX_B];

public const NUMBER_SUFFIX_ABBREVIATION = [
'apartment' => '',
Expand Down Expand Up @@ -81,17 +88,32 @@ public static function splitStreet(string $fullStreet, string $local, string $de
}
}

$regex = ValidateStreet::getStreetRegexByCountry($local, $destination);

if ($destination === AbstractConsignment::CC_BE) {
// Replace box variants to bus
$fullStreet = str_ireplace(self::BOX_SEPARATOR, self::BOX_NL, $fullStreet);
// When a caracter is present at BOX_SEPARATOR_BY_REGEX and followed by a number, it must replaced by bus
preg_match(ValidateStreet::SPLIT_STREET_REGEX_BE, $fullStreet, $matches);

if (in_array($matches['box_separator'], self::BOX_SEPARATOR)) {
$matches['box_separator'] = self::BOX_NL;
}

$fullStreet = implode(
' ',
array_filter([
$matches['street'],
$matches['number'],
$matches['box_separator'],
$matches['box_number'],
$matches['number_suffix'],
])
);

// When a character is present at BOX_SEPARATOR_BY_REGEX and followed by a number, it must be replaced by bus
foreach (self::BOX_SEPARATOR_BY_REGEX as $boxRegex) {
$fullStreet = preg_replace('#' . $boxRegex . '([0-9])#', self::BOX_NL . ' ' . ltrim('$1'), $fullStreet);
}
}

$regex = ValidateStreet::getStreetRegexByCountry($local, $destination);

if (! $regex) {
return new FullStreet($fullStreet, null, null, null);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Helper/ValidateStreet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class ValidateStreet
')?$~i';

const SPLIT_STREET_REGEX_BE =
'~(?P<street>.*?)\s(?P<street_suffix>(?P<number>[0-9\-]{1,8})\s?(?P<box_separator>' . SplitStreet::BOX_NL . ')?\s?(?P<box_number>\d{0,8})\s?(?P<number_suffix>[A-z]{0,4}$)?)$~';
'~(?P<street>.*?)\s(?P<street_suffix>(?P<number>[0-9\-]{1,8})\s?(?P<box_separator>' . self::REGEX_BE_BOX_SEPARATORS . ')?\s?(?P<box_number>\d{0,8})\s?(?P<number_suffix>[A-z]{0,4}$)?)$~';

const REGEX_BE_BOX_SEPARATORS = SplitStreet::BOX_BTE . '|' . SplitStreet::BOX_EN . '|' . SplitStreet::BOX_FR . '|' . SplitStreet::BOX_NL . '|' . SplitStreet::BOX_DE . '|' . SplitStreet::BOX_SLASH . '|' . SplitStreet::BOX_DASH . '|' . SplitStreet::BOX_B . '.+';
/**
* @param string $fullStreet
* @param string $localCountry
Expand Down

0 comments on commit a2f1e29

Please sign in to comment.