dvdoug /
BoxPacker
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Box packing (3D bin packing, knapsack problem). |
||
| 5 | * |
||
| 6 | * @author Doug Wright |
||
| 7 | */ |
||
| 8 | declare(strict_types=1); |
||
| 9 | |||
| 10 | namespace DVDoug\BoxPacker; |
||
| 11 | |||
| 12 | use JsonSerializable; |
||
| 13 | |||
| 14 | use function is_iterable; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * A packed item. |
||
| 18 | */ |
||
| 19 | readonly class PackedItem implements JsonSerializable |
||
| 20 | { |
||
| 21 | public int $volume; |
||
| 22 | 102 | ||
| 23 | public function __construct( |
||
| 24 | public Item $item, |
||
| 25 | public int $x, |
||
| 26 | public int $y, |
||
| 27 | public int $z, |
||
| 28 | public int $width, |
||
| 29 | public int $length, |
||
| 30 | public int $depth |
||
| 31 | 102 | ) { |
|
| 32 | } |
||
| 33 | 98 | ||
| 34 | public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self |
||
| 35 | 98 | { |
|
| 36 | 98 | return new self( |
|
| 37 | 98 | $orientatedItem->item, |
|
| 38 | 98 | $x, |
|
| 39 | 98 | $y, |
|
| 40 | 98 | $z, |
|
| 41 | 98 | $orientatedItem->width, |
|
| 42 | 98 | $orientatedItem->length, |
|
| 43 | 98 | $orientatedItem->depth, |
|
| 44 | ); |
||
| 45 | } |
||
| 46 | 3 | ||
| 47 | public function jsonSerialize(): array |
||
| 48 | 3 | { |
|
| 49 | $userValues = []; |
||
| 50 | 3 | ||
| 51 | 2 | if ($this->item instanceof JsonSerializable) { |
|
| 52 | 2 | $userSerialisation = $this->item->jsonSerialize(); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 53 | 1 | if (is_iterable($userSerialisation)) { |
|
| 54 | $userValues = $userSerialisation; |
||
| 55 | 1 | } else { |
|
| 56 | $userValues = ['extra' => $userSerialisation]; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | 3 | ||
| 60 | 3 | return [ |
|
| 61 | 3 | 'x' => $this->x, |
|
| 62 | 3 | 'y' => $this->y, |
|
| 63 | 3 | 'z' => $this->z, |
|
| 64 | 3 | 'width' => $this->width, |
|
| 65 | 3 | 'length' => $this->length, |
|
| 66 | 3 | 'depth' => $this->depth, |
|
| 67 | 3 | 'item' => [ |
|
| 68 | 3 | ...$userValues, |
|
| 69 | 3 | 'description' => $this->item->getDescription(), |
|
| 70 | 3 | 'width' => $this->item->getWidth(), |
|
| 71 | 3 | 'length' => $this->item->getLength(), |
|
| 72 | 3 | 'depth' => $this->item->getDepth(), |
|
| 73 | 3 | 'allowedRotation' => $this->item->getAllowedRotation(), |
|
| 74 | 3 | ], |
|
| 75 | ]; |
||
| 76 | } |
||
| 77 | } |
||
| 78 |