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