Passed
Push — master ( e748d4...4fe23d )
by Doug
02:40
created

PackedItem::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
ccs 0
cts 12
cp 0
crap 2
rs 9.8333
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
/**
14
 * A packed item.
15
 *
16
 * @author Doug Wright
17
 */
18
class PackedItem implements JsonSerializable
19
{
20
    /**
21
     * @var int
22
     */
23
    protected $x;
24
25
    /**
26
     * @var int
27
     */
28
    protected $y;
29
30
    /**
31
     * @var int
32
     */
33
    protected $z;
34
35
    /**
36
     * @var Item
37
     */
38
    protected $item;
39
40
    /**
41
     * @var int
42
     */
43
    protected $width;
44
45
    /**
46
     * @var int
47
     */
48
    protected $length;
49
50
    /**
51
     * @var int
52
     */
53
    protected $depth;
54
55
    /**
56
     * PackedItem constructor.
57
     */
58 74
    public function __construct(Item $item, int $x, int $y, int $z, int $width, int $length, int $depth)
59
    {
60 74
        $this->item = $item;
61 74
        $this->x = $x;
62 74
        $this->y = $y;
63 74
        $this->z = $z;
64 74
        $this->width = $width;
65 74
        $this->length = $length;
66 74
        $this->depth = $depth;
67 74
    }
68
69 72
    public function getX(): int
70
    {
71 72
        return $this->x;
72
    }
73
74 72
    public function getY(): int
75
    {
76 72
        return $this->y;
77
    }
78
79 72
    public function getZ(): int
80
    {
81 72
        return $this->z;
82
    }
83
84 72
    public function getItem(): Item
85
    {
86 72
        return $this->item;
87
    }
88
89 72
    public function getWidth(): int
90
    {
91 72
        return $this->width;
92
    }
93
94 72
    public function getLength(): int
95
    {
96 72
        return $this->length;
97
    }
98
99 72
    public function getDepth(): int
100
    {
101 72
        return $this->depth;
102
    }
103
104 72
    public function getVolume(): int
105
    {
106 72
        return $this->width * $this->length * $this->depth;
107
    }
108
109
    /**
110
     * @return PackedItem
111
     */
112 70
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
113
    {
114 70
        return new static(
115 70
            $orientatedItem->getItem(),
116
            $x,
117
            $y,
118
            $z,
119 70
            $orientatedItem->getWidth(),
120 70
            $orientatedItem->getLength(),
121 70
            $orientatedItem->getDepth()
122
        );
123
    }
124
125
    public function toOrientatedItem(): OrientatedItem
126
    {
127
        return new OrientatedItem($this->item, $this->width, $this->length, $this->depth);
128
    }
129
130
    public function jsonSerialize(): array
131
    {
132
        return [
133
            'x' => $this->x,
134
            'y' => $this->y,
135
            'z' => $this->z,
136
            'width' => $this->width,
137
            'length' => $this->length,
138
            'depth' => $this->depth,
139
            'item' => [
140
                'description' => $this->item->getDescription(),
141
                'width' => $this->item->getWidth(),
142
                'length' => $this->item->getLength(),
143
                'depth' => $this->item->getDepth(),
144
                'allowedRotations' => $this->item->getAllowedRotations(),
145
            ],
146
        ];
147
    }
148
}
149