-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add physical properties properly (#483)
- Loading branch information
1 parent
2610554
commit d7e508c
Showing
5 changed files
with
214 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |