Skip to content

Instantly share code, notes, and snippets.

@reindert-vetter
Last active July 2, 2018 16:40
Show Gist options
  • Save reindert-vetter/e796d272c4c7e567f87d4325896bbd8e to your computer and use it in GitHub Desktop.
Save reindert-vetter/e796d272c4c7e567f87d4325896bbd8e to your computer and use it in GitHub Desktop.
<?php // php 7; declare(strict_types=1);
/**
* For Dutch consignments the street should be divided into name, number and addition.
*
* If you want to improve this regex, please check the test.
* And also adapt these adjustments to our GitHub:
* https://github.com/myparcelnl/sdk
*
* @author Reindert Vetter <[email protected]>
* @copyright MyParcel
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
* @since Regex available since Release MyParcelNL/SDK v0.1.0
*/
namespace Shipment\Service\ExternalShipmentService;
/**
* Class SplitStreetService
*
* @package Shipment\Service\ExternalShipmentService
*/
class SplitStreetService
{
/**
* Regular expression used to split street name from house number.
*
* For supported addresses go to:
* @link test/Unit/Shipment/Service/SplitStreetServiceTest.php
*/
const SPLIT_STREET_REGEX = '~(?P<street>.*?)'. // The rest belongs to the street
'\s?'. // Separator between street and number
'(?P<number>\d{1,4})'. // Number can contain a maximum of 4 numbers
'[/\s-]{0,2}'. // Separators between number and addition
'(?P<number_suffix>'.
'[\D]{0,4}|'. // Number suffix has up to 4 letters or
'\w{1}\d{1,3}|'. // starts with a letter followed by numbers or
'\d{2}\w{1,2}'. // starts with 2 numbers followed by letters
')$~';
/**
* @param string $country
* @param string $fullStreet
*
* @return array
* @throws SystemException
*/
public function getStreetData(string $country, string $fullStreet) // php 7; : array
{
if ($country === 'NL') {
return $this->getDutchStreetData($fullStreet);
}
return [
'street' => $fullStreet,
'number' => '',
'number_suffix' => '',
];
}
/**
* Splits street data into separate parts for street name, house number and extension.
* Only for Dutch addresses
*
* @param string $fullStreet The full street name including all parts
*
* @return array
* @throws SystemException
*/
public function getDutchStreetData(string $fullStreet) // php 7; : array
{
$result = preg_match(self::SPLIT_STREET_REGEX, $fullStreet, $matches);
if (!$result || !is_array($matches)) {
throw new \Exception('This address can not be split. Please check address: ' . $fullStreet);
}
if ($fullStreet != $matches[0]) {
// Characters are gone by preg_match
throw new \Exception(
"Something went wrong with splitting up address: $fullStreet. " .
"See split_street.log for more information"
);
}
return [
'street' => $matches['street'],
'number' => $matches['number'],
'number_suffix' => trim($matches['number_suffix']),
];
}
}
<?php // php 7; declare(strict_types=1);
/**
* For Dutch consignments the street should be divided into name, number and addition. This code tests whether the
* street is split properly.
*
* If you want to add improvements, please create a fork in our GitHub:
* https://github.com/myparcelnl/sdk
*
* @author Reindert Vetter <[email protected]>
* @copyright MyParcel
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
* @since File available since Release MyParcelNL/SDK v0.1.0
*/
use Test\TestCase;
use Shipment\Service\ExternalShipmentService\SplitStreetService;
/**
* Class SplitStreetServiceTest
*/
class SplitStreetServiceTest extends TestCase
{
/**
* @dataProvider additionProvider()
* @runInSeparateProcess
*/
public function testGetStreetData($fullStreet, $street, $number, $numberSuffix, $country)
{
$consignment = (new SplitStreetService())->getStreetData($country, $fullStreet);
self::assertSame($street, $consignment['street'], 'Street: ' . $fullStreet);
self::assertSame($number, $consignment['number'], 'Number from: ' . $fullStreet);
self::assertSame($numberSuffix, $consignment['number_suffix'], 'Number suffix from: ' . $fullStreet);
}
/**
* Data for the test
*
* @return array
*/
public function additionProvider() // php 7; : array
{
return [
[
'full_street' => '3b',
'street' => '',
'number' => '3',
'number_suffix' => 'b',
'country' => 'NL',
],
[
'full_street' => 'Plein 1940-45 3b',
'street' => 'Plein 1940-45',
'number' => '3',
'number_suffix' => 'b',
'country' => 'NL',
],
[
'full_street' => '300 laan 3',
'street' => '300 laan',
'number' => '3',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'A.B.C. street 12',
'street' => 'A.B.C. street',
'number' => '12',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'street street 269-133',
'street' => 'street street',
'number' => '269',
'number_suffix' => '133',
'country' => 'NL',
],
[
'full_street' => 'Abeelstreet H10',
'street' => 'Abeelstreet H',
'number' => '10',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'street street 269-1001',
'street' => 'street street',
'number' => '269',
'number_suffix' => '1001',
'country' => 'NL',
],
[
'full_street' => 'Meijhorst 50e 26',
'street' => 'Meijhorst 50e',
'number' => '26',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'street street 12 ZW',
'street' => 'street street',
'number' => '12',
'number_suffix' => 'ZW',
'country' => 'NL',
],
[
'full_street' => 'street 12',
'street' => 'street',
'number' => '12',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'Biltstreet 113 A BS',
'street' => 'Biltstreet',
'number' => '113',
'number_suffix' => 'A BS',
'country' => 'NL',
],
[
'full_street' => 'Zonegge 23 12',
'street' => 'Zonegge',
'number' => '23',
'number_suffix' => '12',
'country' => 'NL',
],
[
'full_street' => 'Markerkant 10 142',
'street' => 'Markerkant',
'number' => '10',
'number_suffix' => '142',
'country' => 'NL',
],
[
'full_street' => 'Markerkant 10 11e',
'street' => 'Markerkant',
'number' => '10',
'number_suffix' => '11e',
'country' => 'NL',
],
[
'full_street' => 'Sir Winston Churchillln 283 F008',
'street' => 'Sir Winston Churchillln',
'number' => '283',
'number_suffix' => 'F008',
'country' => 'NL',
],
[
'full_street' => 'Sir Winston Churchilllaan 283 59',
'street' => 'Sir Winston Churchilllaan',
'number' => '283',
'number_suffix' => '59',
'country' => 'NL',
],
[
'full_street' => 'Insulindestreet 69 B03',
'street' => 'Insulindestreet',
'number' => '69',
'number_suffix' => 'B03',
'country' => 'NL',
],
[
'full_street' => 'Insulindestreet 69 B3',
'street' => 'Insulindestreet',
'number' => '69',
'number_suffix' => 'B3',
'country' => 'NL',
],
[
'full_street' => 'Ir. Mr. Dr. van Waterschoot van der Grachtstraat in Heerlen 6',
'street' => 'Ir. Mr. Dr. van Waterschoot van der Grachtstraat in Heerlen',
'number' => '6',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'Scheepvaartlaan 34/302',
'street' => 'Scheepvaartlaan',
'number' => '34',
'number_suffix' => '302',
'country' => 'NL',
],
[
'full_street' => 'oan e dijk 48',
'street' => 'oan e dijk',
'number' => '48',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'Vlinderveen137',
'street' => 'Vlinderveen',
'number' => '137',
'number_suffix' => '',
'country' => 'NL',
],
[
'full_street' => 'street 39-1hg',
'street' => 'street 39-',
'number' => '1',
'number_suffix' => 'hg',
'country' => 'NL',
],
[
'full_street' => 'Nicolaas Ruyschstraat 8 02L',
'street' => 'Nicolaas Ruyschstraat',
'number' => '8',
'number_suffix' => '02L',
'country' => 'NL',
],
[
'full_street' => 'Landsdijk 49 -A',
'street' => 'Landsdijk',
'number' => '49',
'number_suffix' => 'A',
'country' => 'NL',
],
[
'full_street' => 'No. 7 street',
'street' => 'No. 7 street',
'number' => '',
'number_suffix' => '',
'country' => 'FR',
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment