| 1 |
<?php declare(strict_types=1); |
| 2 |
/** |
| 3 |
* If you want to add improvements, please create a fork in our GitHub: |
| 4 |
* https://github.com/myparcelnl |
| 5 |
* |
| 6 |
* @author Reindert Vetter <[email protected]> |
| 7 |
* @copyright 2010-2020 MyParcel |
| 8 |
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL |
| 9 |
* @link https://github.com/myparcelnl/sdk |
| 10 |
* @since File available since Release v2.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace MyParcelNL\Sdk\src\Helper; |
| 14 |
|
| 15 |
use MyParcelNL\Sdk\src\Exception\InvalidConsignmentException; |
| 16 |
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment; |
| 17 |
use MyParcelNL\Sdk\src\Model\FullStreet; |
| 18 |
|
| 19 |
class SplitStreet |
| 20 |
{ |
| 21 |
const BOX_NL = 'bus'; |
| 22 |
const BOX_SEPARATOR = ['boîte', 'box', 'bte', 'Bus']; |
| 23 |
const BOX_SEPARATOR_BY_REGEX = ['\/','-', 'B']; |
| 24 |
|
| 25 |
public const NUMBER_SUFFIX_ABBREVIATION = [ |
| 26 |
'apartment' => '', |
| 27 |
'gedempte' => 'GED', |
| 28 |
'groot' => 'GRT', |
| 29 |
'grote' => 'GRT', |
| 30 |
'greate' => 'GRT', |
| 31 |
'noordzijde' => 'NZ', |
| 32 |
'oostzijde' => 'OZ', |
| 33 |
'zuidzijde' => 'ZZ', |
| 34 |
'westzijde' => 'WZ', |
| 35 |
'noord' => 'N', |
| 36 |
'oost' => 'O', |
| 37 |
'zuid' => 'Z', |
| 38 |
'west' => 'W', |
| 39 |
'hoog' => 'HG', |
| 40 |
'hoge' => 'HG', |
| 41 |
'hege' => 'HG', |
| 42 |
'kleine' => 'KL', |
| 43 |
'klein' => 'KL', |
| 44 |
'korte' => 'K', |
| 45 |
'kort' => 'K', |
| 46 |
'koarte' => 'K', |
| 47 |
'koart' => 'K', |
| 48 |
'kromme' => 'KR', |
| 49 |
'krom' => 'KR', |
| 50 |
'laag' => 'LG', |
| 51 |
'lage' => 'LG', |
| 52 |
'lege' => 'LG', |
| 53 |
'lange' => 'L', |
| 54 |
'lang' => 'L', |
| 55 |
'nieuwe' => 'NW', |
| 56 |
'nieuw' => 'NW', |
| 57 |
'verlengde' => 'VERL', |
| 58 |
]; |
| 59 |
|
| 60 |
/** |
| 61 |
* Splits street data into separate parts for street name, house number and extension. |
| 62 |
* Only for Dutch and Belgium addresses |
| 63 |
* |
| 64 |
* @param string $fullStreet The full street name including all parts |
| 65 |
* |
| 66 |
* @param string $local |
| 67 |
* @param string $destination |
| 68 |
* |
| 69 |
* @return \MyParcelNL\Sdk\src\Model\FullStreet |
| 70 |
* |
| 71 |
* @throws \Exception |
| 72 |
*/ |
| 73 |
public static function splitStreet(string $fullStreet, string $local, string $destination): FullStreet |
| 74 |
{ |
| 75 |
$fullStreet = trim(preg_replace('/(\r\n)|\n|\r/', ' ', $fullStreet)); |
| 76 |
|
| 77 |
// Replace house number suffix by an abbreviation, only possible for the Netherlands |
| 78 |
if ($destination === AbstractConsignment::CC_NL) { |
| 79 |
foreach (self::NUMBER_SUFFIX_ABBREVIATION as $from => $to) { |
| 80 |
$fullStreet = preg_replace("/(\d.*-?)[\s]$from/", '$1' . $to, $fullStreet); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ($destination === AbstractConsignment::CC_BE) { |
| 85 |
// Replace box variants to bus |
| 86 |
$fullStreet = str_ireplace(self::BOX_SEPARATOR, self::BOX_NL, $fullStreet); |
| 87 |
// When a caracter is present at BOX_SEPARATOR_BY_REGEX and followed by a number, it must replaced by bus |
| 88 |
foreach (self::BOX_SEPARATOR_BY_REGEX as $boxRegex) { |
| 89 |
$fullStreet = preg_replace('#' . $boxRegex . '([0-9])#', self::BOX_NL . ' ' . ltrim('$1'), $fullStreet); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$regex = ValidateStreet::getStreetRegexByCountry($local, $destination); |
| 94 |
|
| 95 |
if (! $regex) { |
| 96 |
return new FullStreet($fullStreet, null, null, null); |
| 97 |
} |
| 98 |
|
| 99 |
$result = preg_match($regex, $fullStreet, $matches); |
| 100 |
self::validate($fullStreet, $result, $matches); |
| 101 |
|
| 102 |
return new FullStreet( |
| 103 |
$matches['street'] ?? $fullStreet, |
| 104 |
(int) $matches['number'] ?? null, |
| 105 |
$matches['number_suffix'] ?? null, |
| 106 |
$matches['box_number'] ?? null |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Wraps a street to max street length |
| 112 |
* |
| 113 |
* @param $street |
| 114 |
* |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public static function getStreetParts($street) |
| 118 |
{ |
| 119 |
$streetWrap = wordwrap($street, AbstractConsignment::MAX_STREET_LENGTH, 'BREAK_LINE'); |
| 120 |
$parts = explode("BREAK_LINE", $streetWrap); |
| 121 |
|
| 122 |
return $parts; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @param string $fullStreet |
| 127 |
* @param string $localCountry |
| 128 |
* @param string|null $destinationCountry |
| 129 |
* |
| 130 |
* @return bool |
| 131 |
* @deprecated use ValidateStreet::validate instead |
| 132 |
*/ |
| 133 |
public static function isCorrectStreet(string $fullStreet, string $localCountry, ?string $destinationCountry): bool |
| 134 |
{ |
| 135 |
return ValidateStreet::validate($fullStreet, $localCountry, $destinationCountry); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param string $local |
| 140 |
* @param string $destination |
| 141 |
* |
| 142 |
* @return null|string |
| 143 |
* @deprecated use ValidateStreet::getStreetRegexByCountry instead |
| 144 |
*/ |
| 145 |
public static function getRegexByCountry(string $local, string $destination): ?string |
| 146 |
{ |
| 147 |
return ValidateStreet::getStreetRegexByCountry($local, $destination); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @param string $fullStreet |
| 152 |
* @param int $result |
| 153 |
* @param array $matches |
| 154 |
* |
| 155 |
* @return void |
| 156 |
* @throws \MyParcelNL\Sdk\src\Exception\InvalidConsignmentException |
| 157 |
*/ |
| 158 |
private static function validate(string $fullStreet, int $result, array $matches): void |
| 159 |
{ |
| 160 |
if (! $result || ! is_array($matches)) { |
| 161 |
// Invalid full street supplied |
| 162 |
throw new InvalidConsignmentException('Invalid full street supplied: ' . $fullStreet); |
| 163 |
} |
| 164 |
|
| 165 |
if ($fullStreet != $matches[0]) { |
| 166 |
// Characters are gone by preg_match |
| 167 |
throw new InvalidConsignmentException('Something went wrong splitting up the following address: ' . $fullStreet); |
| 168 |
} |
| 169 |
|
| 170 |
return; |
| 171 |
} |
| 172 |
} |
| 173 |
|