From a2f1e29b55d94be22b208869a88af6281fb2b544 Mon Sep 17 00:00:00 2001 From: Mark Ernst <33826576+Mark-Ernst@users.noreply.github.com> Date: Thu, 26 Oct 2023 10:54:17 +0200 Subject: [PATCH] fix(consignment): replace part of box separator instead of fullstreet (#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 --- src/Helper/SplitStreet.php | 36 ++++++++++++++++++++++++++++------- src/Helper/ValidateStreet.php | 3 ++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/Helper/SplitStreet.php b/src/Helper/SplitStreet.php index 7d907dd0..403578b7 100644 --- a/src/Helper/SplitStreet.php +++ b/src/Helper/SplitStreet.php @@ -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' => '', @@ -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); } diff --git a/src/Helper/ValidateStreet.php b/src/Helper/ValidateStreet.php index 15f985bb..79892ba3 100644 --- a/src/Helper/ValidateStreet.php +++ b/src/Helper/ValidateStreet.php @@ -25,8 +25,9 @@ class ValidateStreet ')?$~i'; const SPLIT_STREET_REGEX_BE = - '~(?P.*?)\s(?P(?P[0-9\-]{1,8})\s?(?P' . SplitStreet::BOX_NL . ')?\s?(?P\d{0,8})\s?(?P[A-z]{0,4}$)?)$~'; + '~(?P.*?)\s(?P(?P[0-9\-]{1,8})\s?(?P' . self::REGEX_BE_BOX_SEPARATORS . ')?\s?(?P\d{0,8})\s?(?P[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