| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MyParcelNL\Sdk\src\Helper; |
| 4 |
|
| 5 |
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment; |
| 6 |
|
| 7 |
class ValidateStreet |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Regular expression used to split street name from house number for the Netherlands. |
| 11 |
* |
| 12 |
* This regex goes from right to left |
| 13 |
* Contains php keys to store the data in an array |
| 14 |
*/ |
| 15 |
const SPLIT_STREET_REGEX_NL = |
| 16 |
'~(?P<street>.{1,78}?)' . // The rest belongs to the street |
| 17 |
'\s' . // Separator between street and number |
| 18 |
'(?P<number>\d{1,5})' . // Number can contain a maximum of 5 numbers |
| 19 |
'[/\s\-]{0,2}' . // Separators between number and addition |
| 20 |
'(?P<number_suffix>' . |
| 21 |
'[a-z]{1}-?\d{1,3}|' . // Numbers suffix starts with a letter with optional - followed by numbers or |
| 22 |
'-\d{1,4}|' . // starts with - and has up to 4 numbers or |
| 23 |
'\d{2}\w{1,2}|' . // starts with 2 numbers followed by letters or |
| 24 |
'[a-z]{1}[a-z\s]{0,3}' . // has up to 4 letters with a space |
| 25 |
')?$~i'; |
| 26 |
|
| 27 |
const SPLIT_STREET_REGEX_BE = |
| 28 |
'~(?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}$)?)$~'; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param string $fullStreet |
| 32 |
* @param string $localCountry |
| 33 |
* @param string|null $destinationCountry |
| 34 |
* |
| 35 |
* @return bool |
| 36 |
*/ |
| 37 |
public static function validate(string $fullStreet, string $localCountry, ?string $destinationCountry): bool |
| 38 |
{ |
| 39 |
if (null === $destinationCountry) { |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
$regex = ValidateStreet::getStreetRegexByCountry($localCountry, $destinationCountry); |
| 44 |
|
| 45 |
if (! $regex) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
$result = preg_match($regex, $fullStreet, $matches); |
| 50 |
|
| 51 |
if (! $result || ! is_array($matches)) { |
| 52 |
// Invalid full street supplied |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
$fullStreet = str_replace('\n', ' ', $fullStreet); |
| 57 |
if ($fullStreet != $matches[0]) { |
| 58 |
// Characters are gone by preg_match |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return (bool) $result; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param string $local |
| 67 |
* @param string $destination |
| 68 |
* |
| 69 |
* @return null|string |
| 70 |
*/ |
| 71 |
public static function getStreetRegexByCountry(string $local, string $destination): ?string |
| 72 |
{ |
| 73 |
$localIsBe = $local === AbstractConsignment::CC_BE; |
| 74 |
$localIsNlOrBe = in_array($local, [AbstractConsignment::CC_BE, AbstractConsignment::CC_NL]); |
| 75 |
$destinationIsNl = $destination === AbstractConsignment::CC_NL; |
| 76 |
$destinationIsBe = $destination === AbstractConsignment::CC_BE; |
| 77 |
|
| 78 |
if ($localIsNlOrBe && $destinationIsNl) { |
| 79 |
return self::SPLIT_STREET_REGEX_NL; |
| 80 |
} |
| 81 |
|
| 82 |
if ($localIsBe && $destinationIsBe) { |
| 83 |
return self::SPLIT_STREET_REGEX_BE; |
| 84 |
} |
| 85 |
|
| 86 |
return null; |
| 87 |
} |
| 88 |
} |
| 89 |
|