Skip to content

Commit

Permalink
Backport #356 to 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Dec 5, 2022
1 parent 43a4241 commit 548f96f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

## [3.x - Unreleased] - 2022-xx-xx

### Changed
- Calling `json_encode()` on a `PackedBox` or `PackedItem` now additionally serialises the entire underlying
`Box`/`Item` where those objects also implement `JsonSerializable`. Previously the serialisation only included the
key values from the `Box`/`Item` interfaces themselves.

## [3.10.0] - 2022-09-10
### Added
- Added `ItemSorter`, `BoxSorter` and `PackedBoxSorter` to allow calling applications to have better control over
Expand Down
28 changes: 22 additions & 6 deletions src/PackedBox.php
Expand Up @@ -14,6 +14,8 @@
use function iterator_to_array;
use function max;
use function round;
use function array_merge;
use function is_array;

/**
* A "box" with items.
Expand Down Expand Up @@ -216,13 +218,27 @@ public function __construct(Box $box, PackedItemList $packedItemList)
#[ReturnTypeWillChange]
public function jsonSerialize()/* : mixed */
{
$userValues = [];

if ($this->box instanceof JsonSerializable) {
$userSerialisation = $this->box->jsonSerialize();
if (is_array($userSerialisation)) {
$userValues = $userSerialisation;
} else {
$userValues = ['extra' => $userSerialisation];
}
}

return [
'box' => [
'reference' => $this->box->getReference(),
'innerWidth' => $this->box->getInnerWidth(),
'innerLength' => $this->box->getInnerLength(),
'innerDepth' => $this->box->getInnerDepth(),
],
'box' => array_merge(
$userValues,
[
'reference' => $this->box->getReference(),
'innerWidth' => $this->box->getInnerWidth(),
'innerLength' => $this->box->getInnerLength(),
'innerDepth' => $this->box->getInnerDepth(),
]
),
'items' => iterator_to_array($this->items),
];
}
Expand Down
31 changes: 24 additions & 7 deletions src/PackedItem.php
Expand Up @@ -11,6 +11,9 @@
use JsonSerializable;
use ReturnTypeWillChange;

use function array_merge;
use function is_array;

/**
* A packed item.
*
Expand Down Expand Up @@ -131,20 +134,34 @@ public function toOrientatedItem(): OrientatedItem
#[ReturnTypeWillChange]
public function jsonSerialize()/* : mixed */
{
$userValues = [];

if ($this->item instanceof JsonSerializable) {
$userSerialisation = $this->item->jsonSerialize();
if (is_array($userSerialisation)) {
$userValues = $userSerialisation;
} else {
$userValues = ['extra' => $userSerialisation];
}
}

return [
'x' => $this->x,
'y' => $this->y,
'z' => $this->z,
'width' => $this->width,
'length' => $this->length,
'depth' => $this->depth,
'item' => [
'description' => $this->item->getDescription(),
'width' => $this->item->getWidth(),
'length' => $this->item->getLength(),
'depth' => $this->item->getDepth(),
'keepFlat' => $this->item->getKeepFlat(),
],
'item' => array_merge(
$userValues,
[
'description' => $this->item->getDescription(),
'width' => $this->item->getWidth(),
'length' => $this->item->getLength(),
'depth' => $this->item->getDepth(),
'keepFlat' => $this->item->getKeepFlat(),
]
),
];
}
}
2 changes: 1 addition & 1 deletion tests/PackedBoxListTest.php
Expand Up @@ -190,6 +190,6 @@ public function testJsonSerialize(): void
$packedBoxList = new PackedBoxList();
$packedBoxList->insert($packedBox);

self::assertJsonStringEqualsJsonString('[{"box":{"reference":"Box","innerWidth":10,"innerLength":10,"innerDepth":20},"items":[{"x":0,"y":0,"z":0,"width":4,"length":10,"depth":10,"item":{"description":"Item","width":4,"length":10,"depth":10,"keepFlat":true}}]}]', json_encode($packedBoxList));
self::assertJsonStringEqualsJsonString('[{"box":{"reference":"Box","innerWidth":10,"innerLength":10,"innerDepth":20,"maxWeight":10,"emptyWeight":10},"items":[{"x":0,"y":0,"z":0,"width":4,"length":10,"depth":10,"item":{"description":"Item","width":4,"length":10,"depth":10,"keepFlat":true,"weight":10}}]}]', json_encode($packedBoxList));
}
}
2 changes: 1 addition & 1 deletion tests/PackedBoxTest.php
Expand Up @@ -98,6 +98,6 @@ public function testJsonSerialize(): void

$packedBox = new PackedBox($box, $boxItems);

self::assertJsonStringEqualsJsonString('{"box":{"reference":"Box","innerWidth":10,"innerLength":10,"innerDepth":20},"items":[{"x":0,"y":0,"z":0,"width":4,"length":10,"depth":10,"item":{"description":"Item","width":4,"length":10,"depth":10,"keepFlat":true}}]}', json_encode($packedBox));
self::assertJsonStringEqualsJsonString('{"box":{"reference":"Box","innerWidth":10,"innerLength":10,"innerDepth":20,"maxWeight":10,"emptyWeight":10},"items":[{"x":0,"y":0,"z":0,"width":4,"length":10,"depth":10,"item":{"description":"Item","width":4,"length":10,"depth":10,"keepFlat":true,"weight":10}}]}', json_encode($packedBox));
}
}
2 changes: 1 addition & 1 deletion tests/PackedItemTest.php
Expand Up @@ -33,6 +33,6 @@ public function testVolumeCalculation(): void
public function testJsonSerialize(): void
{
$packedItem = new PackedItem(new TestItem('Item', 1, 2, 3, 10, false), 100, 20, 300, 3, 5, 7);
self::assertJsonStringEqualsJsonString('{"x":100,"y":20,"z":300,"width":3,"length":5,"depth":7,"item":{"description":"Item","width":1,"length":2,"depth":3,"keepFlat":false}}', json_encode($packedItem));
self::assertJsonStringEqualsJsonString('{"x":100,"y":20,"z":300,"width":3,"length":5,"depth":7,"item":{"description":"Item","width":1,"length":2,"depth":3,"keepFlat":false, "weight":10}}', json_encode($packedItem));
}
}

0 comments on commit 548f96f

Please sign in to comment.