b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / a / home / dev2.destoffenstraat.com / app / code / TIG / PostNL / Helper /
Filename/home/a/home/dev2.destoffenstraat.com/app/code/TIG/PostNL/Helper/AddressEnhancer.php
Size6.43 kb
Permissionrwxrwxrwx
Ownerroot : root
Create time21-Aug-2025 12:26
Last modified20-Dec-2022 13:13
Last accessed22-Aug-2025 22:33
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php
/**
*
* ..::..
* ..::::::::::::..
* ::'''''':''::'''''::
* ::.. ..: : ....::
* :::: ::: : : ::
* :::: ::: : ''' ::
* ::::..:::..::.....::
* ''::::::::::::''
* ''::''
*
*
* NOTICE OF LICENSE
*
* This source file is subject to the Creative Commons License.
* It is available through the world-wide-web at this URL:
* http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US
* If you are unable to obtain it through the world-wide-web, please send an email
* to servicedesk@tig.nl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please contact servicedesk@tig.nl for more information.
*
* @copyright Copyright (c) Total Internet Group B.V. https://tig.nl/copyright
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US
*/

namespace TIG\PostNL\Helper;

use TIG\PostNL\Config\Provider\Webshop as Config;
use TIG\PostNL\Exception as PostnlException;

class AddressEnhancer
{
// @codingStandardsIgnoreLine
const STREET_SPLIT_NAME_FROM_NUMBER = '/^(?P<street>\d*[\wäöüßÀ-ÖØ-öø-ÿĀ-Ž\d \'\‘\`\-\.]+)[,\s]+(?P<number>\d+)\s*(?P<addition>[\wäöüß\d\-\/]*)$/i';
// @codingStandardsIgnoreLine
const STREET_SPLIT_NUMBER_FROM_NAME = '/^(?P<number>\d+)\s*(?P<street>[\wäöüßÀ-ÖØ-öø-ÿĀ-Ž\d \'\‘\`\-\.]*)$/i';

/** @var Config $config */
private $config;

/** @var array */
// @codingStandardsIgnoreLine
protected $address = [];

/**
* AddressEnhancer constructor.
*
* @param Config $config
*/
public function __construct(
Config $config
) {
$this->config = $config;
}

/**
* @param $address
*
* @throws PostnlException
*/
public function set($address)
{
$this->address = $address;

if ((isset($this->address['country']) && $this->address['country'] !== 'NL') ||
!$this->config->getIsAddressCheckEnabled() ||
// If an address is parsed as a 1-liner, we still have to extract the housenumber
!is_array($address['street']) ||
!isset($address['street'][1])
) {
$this->address = $this->appendHouseNumber($address);
}

if (empty($this->address['housenumber']) && isset($this->address['street'][1])) {
$this->address['housenumber'] = $this->address['street'][1];
}
}

/**
* @return array
*/
public function get()
{
return $this->address;
}

/**
* @param $address
*
* @return mixed
* @throws PostnlException
*/
// @codingStandardsIgnoreLine
protected function appendHouseNumber($address)
{
if (!isset($address['street'][0])) {
return [
'error' => [
'code' => 'POSTNL-0124',
'message' => 'Unable to extract the house number, because the street data could not be found'
]
];
}

if (!isset($address['housenumber']) || !$address['housenumber']) {
$address = $this->extractHousenumber($address);
}

return $address;
}

/**
* @param $address
*
* @return mixed
* @throws PostnlException
*/
// @codingStandardsIgnoreLine
protected function extractHousenumber($address)
{
$street = $address['street'];

if (is_array($address['street'])) {
$street = implode(' ', $address['street']);
}

$matched = preg_match(self::STREET_SPLIT_NAME_FROM_NUMBER, trim($street), $result);
if (!$matched) {
$result = $this->extractStreetFromNumber($street);
}

if (isset($result['error'])) {
$result = $this->extractIndividual($address, $result);

return $result;
}

return $this->parseResult($result, $address);
}

/**
* @param $street
*
* @return array
*/
// @codingStandardsIgnoreLine
protected function extractStreetFromNumber($street)
{
$matched = preg_match(self::STREET_SPLIT_NUMBER_FROM_NAME, trim($street), $result);
if (!$matched) {
return [
'error' => [
'code' => 'POSTNL-0124',
'message' => 'Unable to extract the house number, could not find a number inside the street value'
]
];
}

return $result;
}

/**
* @param $address
* @param $result
*
* @return mixed
* @throws PostnlException
*/
// @codingStandardsIgnoreLine
protected function extractIndividual($address, $result)
{
// @codingStandardsIgnoreLine
if (count($address['street']) == 3) {
$result['street'] = $address['street'][0];
$result['number'] = $address['street'][1];
$result['addition'] = $address['street'][2];
$address['street'][1] = '';
$address['street'][2] = '';
unset($result['error']);
}
// @codingStandardsIgnoreLine
if (count($address['street']) == 2) {
$tmpAddress = $this->extractHousenumber(['street' => [$address['street'][0]]]);
$result['street'] = $address['street'][0];
$result['number'] = isset($tmpAddress['housenumber']) ? $tmpAddress['housenumber'] : null;
$result['addition'] = $address['street'][1];
$address['street'][1] = '';
unset($result['error']);
}

return !isset($result['error']) ? $this->parseResult($result, $address) : $result;
}

/**
* @param $result
* @param $address
*
* @return mixed
*/
// @codingStandardsIgnoreLine
protected function parseResult($result, $address)
{
if (!is_array($result)) {
return $address;
}

if ($result['street']) {
$address['street'][0] = trim($result['street']);
}

if ($result['number']) {
$address['housenumber'] = trim($result['number']);
}

$address['housenumberExtension'] = '';
if (isset($result['addition']) && $result['addition']) {
$address['housenumberExtension'] = trim($result['addition']);
}

return $address;
}
}