| 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>.{2,78}?)' . // The rest belongs to the street |
| 17 |
'\s' . // Separator between street and number |
| 18 |
'(?P<number>\d{1,4})' . // Number can contain a maximum of 4 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 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 |
$result = preg_match(ValidateStreet::getStreetRegexByCountry($localCountry, $destinationCountry), $fullStreet, $matches); |
| 40 |
|
| 41 |
if (! $result || ! is_array($matches)) { |
| 42 |
// Invalid full street supplied |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
$fullStreet = str_replace('\n', ' ', $fullStreet); |
| 47 |
if ($fullStreet != $matches[0]) { |
| 48 |
// Characters are gone by preg_match |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
return (bool) $result; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @param string $local |
| 57 |
* @param string $destination |
| 58 |
* |
| 59 |
* @return null|string |
| 60 |
*/ |
| 61 |
public static function getStreetRegexByCountry(string $local, string $destination): ?string |
| 62 |
{ |
| 63 |
$localIsBe = $local === AbstractConsignment::CC_BE; |
| 64 |
$localIsNlOrBe = in_array($local, [AbstractConsignment::CC_BE, AbstractConsignment::CC_NL]); |
| 65 |
$destinationIsNl = $destination === AbstractConsignment::CC_NL; |
| 66 |
$destinationIsBe = $destination === AbstractConsignment::CC_BE; |
| 67 |
|
| 68 |
if ($localIsNlOrBe && $destinationIsNl) { |
| 69 |
return self::SPLIT_STREET_REGEX_NL; |
| 70 |
} |
| 71 |
|
| 72 |
if ($localIsBe && $destinationIsBe) { |
| 73 |
return self::SPLIT_STREET_REGEX_BE; |
| 74 |
} |
| 75 |
|
| 76 |
return null; |
| 77 |
} |
| 78 |
} |
| 79 |
|