Skip to content

Commit

Permalink
feat: add physical properties properly (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen authored Jun 21, 2024
1 parent 2610554 commit d7e508c
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 14 deletions.
7 changes: 1 addition & 6 deletions src/Collection/Fulfilment/OrderCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ function (Order $order) {
'pickup' => $order->getPickupLocation() ? $order->getPickupLocation()->toArrayWithoutNull() : null,
'drop_off_point' => $dropOffPointAsArray,
'customs_declaration' => $order->getCustomsDeclaration(),
'physical_properties' => [
'weight' => $order->getWeight(),
'height' => 1,
'length' => 1,
'width' => 1,
],
'physical_properties' => $order->getPhysicalProperties()->toArray(),
],
];
}
Expand Down
35 changes: 34 additions & 1 deletion src/Model/Fulfilment/AbstractOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory;
use MyParcelNL\Sdk\src\Model\Consignment\DropOffPoint;
use MyParcelNL\Sdk\src\Model\CustomsDeclaration;
use MyParcelNL\Sdk\src\Model\PhysicalProperties;
use MyParcelNL\Sdk\src\Model\PickupLocation;
use MyParcelNL\Sdk\src\Model\Recipient;
use MyParcelNL\Sdk\src\Support\Collection;
Expand Down Expand Up @@ -91,6 +92,11 @@ class AbstractOrder extends BaseModel
*/
protected $pickupLocation;

/**
* @var \MyParcelNL\Sdk\src\Model\PhysicalProperties|null
*/
protected $physicalProperties;

/**
* @var string|null
*/
Expand Down Expand Up @@ -235,6 +241,20 @@ public function getRecipient(): Recipient
return $this->recipient;
}

/**
* When not set, will return PhysicalProperties with order total weight and default dimensions.
*
* @return PhysicalProperties
*/
public function getPhysicalProperties(): PhysicalProperties
{
if (!isset($this->physicalProperties)) {
$this->physicalProperties = new PhysicalProperties(['weight'=>$this->getWeight()]);
}

return $this->physicalProperties;
}

/**
* @return \MyParcelNL\Sdk\src\Model\PickupLocation|null
*/
Expand Down Expand Up @@ -391,6 +411,16 @@ public function setRecipient(Recipient $recipient): self
return $this;
}

/**
* @param PhysicalProperties $physicalProperties
* @return self
*/
public function setPhysicalProperties(PhysicalProperties $physicalProperties): self
{
$this->physicalProperties = $physicalProperties;
return $this;
}

/**
* @param \MyParcelNL\Sdk\src\Model\PickupLocation|null $pickupLocation
*
Expand Down Expand Up @@ -473,7 +503,10 @@ public function toArray(): array
],
(null === $this->customs_declaration)
? []
: ['customs_declaration' => $this->getCustomsDeclaration()->toArray()]
: ['customs_declaration' => $this->getCustomsDeclaration()->toArray()],
(null === $this->physicalProperties)
? []
: ['physical_properties' => $this->getPhysicalProperties()->toArray()]
);
}
}
124 changes: 124 additions & 0 deletions src/Model/PhysicalProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);

namespace MyParcelNL\Sdk\src\Model;

class PhysicalProperties extends BaseModel
{
private const DEFAULT_WEIGHT = 10;
private const DEFAULT_LENGTH = 10;
private const DEFAULT_WIDTH = 10;
private const DEFAULT_HEIGHT = 10;


/**
* @var int
*/
private $weight;

/**
* @var int
*/
private $length;

/**
* @var int
*/
private $width;

/**
* @var int
*/
private $height;

/**
* Supply weight, length, width and height. Illegal values will be set to default 10.
* MyParcel API expects weight in grams and dimensions in centimeters / cm.
*
* @param array $data
*/
public function __construct(array $data = [])
{
$this->setWeight(((int) $data['weight']) ?: self::DEFAULT_WEIGHT);
$this->setLength(((int) $data['length']) ?: self::DEFAULT_LENGTH);
$this->setWidth(((int) $data['width']) ?: self::DEFAULT_WIDTH);
$this->setHeight(((int) $data['height']) ?: self::DEFAULT_HEIGHT);
}

/**
* @return int|null
*/
public function getWeight(): ?int
{
return $this->weight;
}

/**
* @param int $weight
*/
public function setWeight(int $weight): void
{
$this->weight = $weight;
}

/**
* @return int|null
*/
public function getLength(): ?int
{
return $this->length;
}

/**
* @param int $length in cm
*/
public function setLength(int $length): void
{
$this->length = $length;
}

/**
* @return int|null
*/
public function getWidth(): ?int
{
return $this->width;
}

/**
* @param int $width in cm
*/
public function setWidth(int $width): void
{
$this->width = $width;
}

/**
* @return int|null
*/
public function getHeight(): ?int
{
return $this->height;
}

/**
* @param int $height in cm
*/
public function setHeight(int $height): void
{
$this->height = $height;
}

/**
* @return array
*/
public function toArray(): array
{
return [
'weight' => $this->getWeight(),
'length' => $this->getLength(),
'width' => $this->getWidth(),
'height' => $this->getHeight(),
];
}
}
7 changes: 0 additions & 7 deletions src/Services/ConsignmentEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,6 @@ private function encodePhysicalProperties(): self
throw new MissingFieldException('Weight in physical properties must be set for digital stamp shipments.');
}

if (CarrierDHLForYou::NAME === $consignment->getCarrier()->getName()) {
$consignment->setPhysicalProperties([
'weight' => $consignment->getTotalWeight(),
'volume' => 1,
]);
}

$this->consignmentEncoded['physical_properties'] = $consignment->getPhysicalProperties();

return $this;
Expand Down
55 changes: 55 additions & 0 deletions test/Model/PhysicalProperties/PhysicalPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Sdk\Test\Model\PhysicalProperties;

use Faker\Factory;
use MyParcelNL\Sdk\src\Model\PhysicalProperties;
use MyParcelNL\Sdk\Test\Bootstrap\TestCase;

class PhysicalPropertiesTest extends TestCase
{
/**
* @var \Faker\Factory
*/
protected $faker;

/**
* @param string $name
* @param array $data
* @param string $dataName
*/
public function __construct($name = null, array $data = [], $dataName = '')
{
$this->faker = Factory::create('nl_NL');
parent::__construct($name, $data, $dataName);
}

public function testDefaults()
{
$physicalProperties = new PhysicalProperties();

$this->assertEquals(10, $physicalProperties->getWeight());
$this->assertEquals(10, $physicalProperties->getLength());
$this->assertEquals(10, $physicalProperties->getWidth());
$this->assertEquals(10, $physicalProperties->getHeight());
}

public function testConstructor()
{
$array = [
'weight' => $this->faker->numberBetween(200, 10000),
'length' => $this->faker->numberBetween(1, 100),
'width' => $this->faker->numberBetween(1, 100),
'height' => $this->faker->numberBetween(1, 100),
];

$physicalProperties = new PhysicalProperties($array);

$this->assertEquals($array['weight'], $physicalProperties->getWeight());
$this->assertEquals($array['length'], $physicalProperties->getLength());
$this->assertEquals($array['width'], $physicalProperties->getWidth());
$this->assertEquals($array['height'], $physicalProperties->getHeight());
}
}

0 comments on commit d7e508c

Please sign in to comment.