| 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 ValidatePostalCode |
| 8 |
{ |
| 9 |
const VALIDATE_POSTAL_CODE_REGEX_NL = '/^[1-9]\d{3}\s?[a-z]{2}$/i'; |
| 10 |
const VALIDATE_POSTAL_CODE_REGEX_BE = '/[1-9]\d{3}$/'; |
| 11 |
|
| 12 |
public static function validate(string $postalCode, ?string $destinationCountry): bool |
| 13 |
{ |
| 14 |
$validatePostalCode = ValidatePostalCode::getPostalCodeRegexByCountry($destinationCountry); |
| 15 |
|
| 16 |
if ($validatePostalCode) { |
| 17 |
return (bool) preg_match($validatePostalCode, $postalCode); |
| 18 |
} |
| 19 |
|
| 20 |
return true; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $local |
| 25 |
* @param string $destination |
| 26 |
* |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public static function getPostalCodeRegexByCountry(string $destination): ?string |
| 30 |
{ |
| 31 |
switch ($destination) { |
| 32 |
case AbstractConsignment::CC_NL: |
| 33 |
return self::VALIDATE_POSTAL_CODE_REGEX_NL; |
| 34 |
case AbstractConsignment::CC_BE: |
| 35 |
return self::VALIDATE_POSTAL_CODE_REGEX_BE; |
| 36 |
default: |
| 37 |
return null; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|