Skip to content

Commit

Permalink
Merge pull request #41 from myparcelnl/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Reindert authored Mar 2, 2018
2 parents 39b0706 + 2962df5 commit 48a7b79
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 22 deletions.
20 changes: 20 additions & 0 deletions Tests/SendConsignments/SendOneConsignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ public function additionProvider()
'return' => false,
'label_description' => 'Label description',
],
[
'api_key' => getenv('API_KEY'),
'cc' => 'NL',
'person' => 'Piet',
'company' => 'Mega Store',
'full_street_test' => 'testtienpp testtienpp testtienpp testtienpp testtienpp 14 t',
'full_street' => 'testtienpp testtienpp testtienpp testtienpp testtienpp 14 t',
'street' => 'testtienpp testtienpp testtienpp testtienpp testtienpp',
'number' => 14,
'number_suffix' => 't',
'postal_code' => '2231JE',
'city' => 'Katwijk',
'phone' => '123-45-235-435',
'package_type' => 1,
'large_format' => false,
'only_recipient' => false,
'signature' => false,
'return' => false,
'label_description' => 'Label description',
],
[
'api_key' => getenv('API_KEY'),
'cc' => 'NL',
Expand Down
91 changes: 91 additions & 0 deletions Tests/SplitStreet/SplitLongStreetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* 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
*
* @author Reindert Vetter <[email protected]>
* @copyright 2010-2017 MyParcel
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
* @link https://github.com/myparcelnl/sdk
* @since File available since Release v0.1.0
*/

namespace MyParcelNL\Sdk\tests\CreateConsignments\SplitStreetTest;
use MyParcelNL\Sdk\src\Model\Repository\MyParcelConsignmentRepository;


/**
* Class SplitStreetTest
* @package MyParcelNL\Sdk\tests\SplitStreetTest
*/
class SplitLongStreetTest extends \PHPUnit_Framework_TestCase
{

/**
* @covers \MyParcelNL\Sdk\src\Model\Repository\MyParcelConsignmentRepository::setFullStreet
* @dataProvider additionProvider()
*/
public function testSplitStreet($country, $fullStreetTest, $street, $streetAdditionalInfo)
{
$consignment = (new MyParcelConsignmentRepository())
->setCountry($country)
->setFullStreet($fullStreetTest);

$this->assertEquals(
$street,
$consignment->getFullStreet(true),
'Street: ' . $street
);

$this->assertEquals(
$streetAdditionalInfo,
$consignment->getStreetAdditionalInfo(),
'Street additional info: ' . $streetAdditionalInfo
);
}

/**
* Data for the test
*
* @return array
*/
public function additionProvider()
{
return [
[
'BE',
'full_street_test' => 'testtienpp testtienpp testtienpp testtienpp testtienpp 14 t',
'street' => 'testtienpp testtienpp testtienpp',
'street_additional_info' => 'testtienpp testtienpp 14 t',
],
[
'BE',
'full_street_test' => 'testtienpp testtienpp',
'street' => 'testtienpp testtienpp',
'street_additional_info' => '',
],
[
'BE',
'full_street_test' => 'Wethouder Fierman Eduard Meerburg senior kade 14 t',
'street' => 'Wethouder Fierman Eduard Meerburg senior',
'street_additional_info' => 'kade 14 t',
],
[
'NL',
'full_street_test' => 'Ir. Mr. Dr. van Waterschoot van der Grachtstraat 14 t',
'street' => 'Ir. Mr. Dr. van Waterschoot van der 14 t',
'street_additional_info' => 'Grachtstraat',
],
[
'NL',
'full_street_test' => 'Koestraat 554 t',
'street' => 'Koestraat 554 t',
'street_additional_info' => '',
],
];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "myparcelnl/sdk",
"version": "v1.3.6",
"version": "v1.3.7",
"description": "This package is designed to send and receive data from MyParcel by means of an API.",
"homepage": "https://www.myparcel.nl",
"tags": ["MyParcel", "My Parcel", "Post NL", "PostNL"],
Expand Down
2 changes: 2 additions & 0 deletions src/Helper/MyParcelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ public function setPdfOfLabels($positions = false)
/**
* Download labels
*
* @param bool $inline_download
*
* @return $this
* @throws \Exception
*/
Expand Down
34 changes: 33 additions & 1 deletion src/Model/MyParcelConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MyParcelConsignment extends MyParcelClassConstants
const DATE_TIME_REGEX = '~(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})$~';
const INSURANCE_POSSIBILITIES = [0, 50, 250, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000];
const STATUS_CONCEPT = 1;
const MAX_STREET_LENTH = 40;

/**
* @var string
Expand Down Expand Up @@ -423,13 +424,34 @@ public function setCity($city)
}

/**
* @var bool
* @return string
*/
public function getStreet()
public function getStreet($useStreetAdditionalInfo = false)
{
if ($useStreetAdditionalInfo && strlen($this->street) >= self::MAX_STREET_LENTH) {
$streetParts = $this->getStreetParts();

return $streetParts[0];
}

return $this->street;
}

/**
* Get additional information for the street that should not be included in the street field
*/
public function getStreetAdditionalInfo()
{
$streetParts = $this->getStreetParts();

if (isset($streetParts[1])) {
return $streetParts[1];
}

return '';
}

/**
* The address street name
*
Expand Down Expand Up @@ -1121,4 +1143,14 @@ private function canHaveOption($option = true)

return $this->getPackageType() == 1 ? $option : false;
}

/**
* Wraps a street to max street lenth
*
* @return array
*/
private function getStreetParts ()
{
return explode("\n", wordwrap($this->street, self::MAX_STREET_LENTH));
}
}
57 changes: 37 additions & 20 deletions src/Model/Repository/MyParcelConsignmentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,34 @@ class MyParcelConsignmentRepository extends MyParcelConsignment
*/
const SPLIT_STREET_REGEX = '~(?P<street>.*?)\s?(?P<street_suffix>(?P<number>[\d]+)[\s-]{0,2}(?P<number_suffix>[a-zA-Z/\s]{0,5}$|[0-9/]{0,5}$|\s[a-zA-Z]{1}[0-9]{0,3}$|\s[0-9]{2}[a-zA-Z]{0,3}$))$~';

/**
* Consignment types
*/
const DELIVERY_TYPE_MORNING = 1;
const DELIVERY_TYPE_STANDARD = 2;
const DELIVERY_TYPE_NIGHT = 3;
const DELIVERY_TYPE_RETAIL = 4;
const DELIVERY_TYPE_RETAIL_EXPRESS = 5;

const PACKAGE_TYPE_NORMAL = 2;

const DEFAULT_PACKAGE_TYPE = self::PACKAGE_TYPE_NORMAL;

/**
* @var array
*/
private $consignmentEncoded = [];
private $consignmentEncoded = [];

/**
* Get entire street
*
* @var bool
*
* @return string Entire street
*/
public function getFullStreet()
public function getFullStreet($useStreetAdditionalInfo = false)
{
$fullStreet = $this->getStreet();
$fullStreet = $this->getStreet($useStreetAdditionalInfo);

if ($this->getNumber()) {
$fullStreet .= ' ' . $this->getNumber();
Expand Down Expand Up @@ -150,34 +165,34 @@ public function apiDecode($data)
public function getDeliveryTypeFromCheckout($checkoutData)
{
if ($checkoutData === null) {
return self::TYPE_STANDARD;
return self::DELIVERY_TYPE_STANDARD;
}

$aCheckoutData = json_decode($checkoutData, true);
$deliveryType = self::TYPE_STANDARD;
$deliveryType = self::DELIVERY_TYPE_STANDARD;

if (key_exists('time', $aCheckoutData) &&
key_exists('price_comment', $aCheckoutData['time'][0]) &&
$aCheckoutData['time'][0]['price_comment'] !== null
) {
switch ($aCheckoutData['time'][0]['price_comment']) {
case 'morning':
$deliveryType = self::TYPE_MORNING;
$deliveryType = self::DELIVERY_TYPE_MORNING;
break;
case 'standard':
$deliveryType = self::TYPE_STANDARD;
$deliveryType = self::DELIVERY_TYPE_STANDARD;
break;
case 'night':
$deliveryType = self::TYPE_NIGHT;
$deliveryType = self::DELIVERY_TYPE_NIGHT;
break;
}
} elseif (key_exists('price_comment', $aCheckoutData) && $aCheckoutData['price_comment'] !== null) {
switch ($aCheckoutData['price_comment']) {
case 'retail':
$deliveryType = self::TYPE_RETAIL;
$deliveryType = self::DELIVERY_TYPE_RETAIL;
break;
case 'retailexpress':
$deliveryType = self::TYPE_RETAIL_EXPRESS;
$deliveryType = self::DELIVERY_TYPE_RETAIL_EXPRESS;
break;
}
}
Expand Down Expand Up @@ -392,14 +407,16 @@ private function encodeStreet()
$this->consignmentEncoded,
[
'recipient' => [
'street' => $this->getStreet(),
'street' => $this->getStreet(true),
'street_additional_info' => $this->getStreetAdditionalInfo(),
'number' => $this->getNumber(),
'number_suffix' => $this->getNumberSuffix(),
],
]
);
} else {
$this->consignmentEncoded['recipient']['street'] = $this->getFullStreet();
$this->consignmentEncoded['recipient']['street'] = $this->getFullStreet(true);
$this->consignmentEncoded['recipient']['street_additional_info'] = $this->getStreetAdditionalInfo();
}

return $this;
Expand Down Expand Up @@ -607,15 +624,15 @@ private function decodeExtraOptions($data)
$this->setInsurance($insuranceAmount / 100);
}

if (isset($options['delivery_date'])) {
$this->setDeliveryDate($options['delivery_date']);
}
if (isset($options['delivery_date'])) {
$this->setDeliveryDate($options['delivery_date']);
}

if (isset($options['delivery_type'])) {
$this->setDeliveryType($options['delivery_type']);
} else {
$this->setDeliveryType(self::TYPE_STANDARD);
}
if (isset($options['delivery_type'])) {
$this->setDeliveryType($options['delivery_type']);
} else {
$this->setDeliveryType(self::DEFAULT_PACKAGE_TYPE);
}

return $this;
}
Expand Down

0 comments on commit 48a7b79

Please sign in to comment.