diff --git a/src/Resources/Pieces.php b/src/Resources/Pieces.php index 875e392..1b7b9b7 100644 --- a/src/Resources/Pieces.php +++ b/src/Resources/Pieces.php @@ -5,21 +5,11 @@ class Pieces extends BaseResource { /** - * @var \Mvdnbrk\DhlParcel\Resources\Piece[] - */ - protected $pieces; - - /** - * Create a new Pieces resource. + * The piece items in this collection. * - * @param array $attributes + * @var \Mvdnbrk\DhlParcel\Resources\Piece[] */ - public function __construct($attributes = []) - { - $this->pieces = []; - - parent::__construct($attributes); - } + protected $items = []; /** * Set the pieces. @@ -29,24 +19,24 @@ public function __construct($attributes = []) public function setPiecesAttribute($pieces) { foreach ($pieces as $piece) { - $this->addPiece($piece); + $this->add($piece); } } /** - * Add a piece. + * Add a piece item to this collection. * * @param \Mvdnbrk\DhlParcel\Resources\Piece|array $value */ - public function addPiece($value) + public function add($value) { if ($value instanceof Piece) { - $this->pieces[] = $value; + $this->items[] = $value; return; } - $this->pieces[] = new Piece($value); + $this->items[] = new Piece($value); } /** @@ -56,7 +46,7 @@ public function addPiece($value) */ public function toArray() { - return collect($this->pieces) + return collect($this->items) ->whenEmpty(function ($collection) { return $collection->push(new Piece); }) diff --git a/tests/Unit/Resources/ParcelTest.php b/tests/Unit/Resources/ParcelTest.php index bd2a7c4..2db3067 100644 --- a/tests/Unit/Resources/ParcelTest.php +++ b/tests/Unit/Resources/ParcelTest.php @@ -66,9 +66,9 @@ public function create_a_new_parcel() $this->assertEquals('Doe', $parcel->recipient->last_name); $this->assertEquals('Test Company B.V.', $parcel->sender->company_name); $this->assertEquals('Test 123', $parcel->options->label_description); - $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->pieces[0]->parcel_type); - $this->assertEquals(1, $parcel->pieces->pieces[0]->quantity); - $this->assertEquals(1, $parcel->pieces->pieces[0]->weight); + $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->items[0]->parcel_type); + $this->assertEquals(1, $parcel->pieces->items[0]->quantity); + $this->assertEquals(1, $parcel->pieces->items[0]->weight); $this->assertSame(true, $parcel->options->only_recipient); $this->assertSame(true, $parcel->options->signature); } @@ -215,10 +215,10 @@ public function it_can_set_the_pieces_by_passing_a_pieces_object() 'pieces' => $pieces, ]); - $this->assertInstanceOf(Piece::class, $parcel->pieces->pieces[0]); - $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->pieces[0]->parcel_type); - $this->assertEquals(1, $parcel->pieces->pieces[0]->quantity); - $this->assertEquals(1, $parcel->pieces->pieces[0]->weight); + $this->assertInstanceOf(Piece::class, $parcel->pieces->items[0]); + $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->items[0]->parcel_type); + $this->assertEquals(1, $parcel->pieces->items[0]->quantity); + $this->assertEquals(1, $parcel->pieces->items[0]->weight); } /** @test */ diff --git a/tests/Unit/Resources/PiecesTest.php b/tests/Unit/Resources/PiecesTest.php index 4089008..2598a90 100644 --- a/tests/Unit/Resources/PiecesTest.php +++ b/tests/Unit/Resources/PiecesTest.php @@ -21,9 +21,9 @@ public function creating_a_new_pieces_resource_with_array() ], ]); - $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type); - $this->assertEquals(1, $pieces->pieces[0]->quantity); - $this->assertEquals(1, $pieces->pieces[0]->weight); + $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type); + $this->assertEquals(1, $pieces->items[0]->quantity); + $this->assertEquals(1, $pieces->items[0]->weight); } /** @test */ @@ -35,8 +35,8 @@ public function creating_a_new_pieces_resource_with_array_of_piece_objects() ], ]); - $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type); - $this->assertEquals(1, $pieces->pieces[0]->quantity); + $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type); + $this->assertEquals(1, $pieces->items[0]->quantity); } /** @test */ @@ -44,10 +44,10 @@ public function it_can_add_a_new_piece_object() { $pieces = new Pieces; - $pieces->addPiece(new Piece); + $pieces->add(new Piece); - $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type); - $this->assertEquals(1, $pieces->pieces[0]->quantity); + $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type); + $this->assertEquals(1, $pieces->items[0]->quantity); } /** @test */ @@ -55,15 +55,15 @@ public function it_can_add_a_new_piece_with_an_array() { $pieces = new Pieces; - $pieces->addPiece([ + $pieces->add([ 'parcel_type' => Piece::PARCEL_TYPE_SMALL, 'quantity' => 1, 'weight' => 1, ]); - $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type); - $this->assertEquals(1, $pieces->pieces[0]->quantity); - $this->assertEquals(1, $pieces->pieces[0]->weight); + $this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type); + $this->assertEquals(1, $pieces->items[0]->quantity); + $this->assertEquals(1, $pieces->items[0]->weight); } /** @test */