Passed
Push — master ( b79dbd...29a054 )
by Doug
08:25 queued 05:38
created

PackedItem::toOrientatedItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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 82
    public function __construct(Item $item, int $x, int $y, int $z, int $width, int $length, int $depth)
59
    {
60 82
        $this->item = $item;
61 82
        $this->x = $x;
62 82
        $this->y = $y;
63 82
        $this->z = $z;
64 82
        $this->width = $width;
65 82
        $this->length = $length;
66 82
        $this->depth = $depth;
67 82
    }
68
69 78
    public function getX(): int
70
    {
71 78
        return $this->x;
72
    }
73
74 78
    public function getY(): int
75
    {
76 78
        return $this->y;
77
    }
78
79 78
    public function getZ(): int
80
    {
81 78
        return $this->z;
82
    }
83
84 78
    public function getItem(): Item
85
    {
86 78
        return $this->item;
87
    }
88
89 78
    public function getWidth(): int
90
    {
91 78
        return $this->width;
92
    }
93
94 78
    public function getLength(): int
95
    {
96 78
        return $this->length;
97
    }
98
99 78
    public function getDepth(): int
100
    {
101 78
        return $this->depth;
102
    }
103
104 78
    public function getVolume(): int
105
    {
106 78
        return $this->width * $this->length * $this->depth;
107
    }
108
109
    /**
110
     * @return PackedItem
111
     */
112 76
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
113
    {
114 76
        return new static(
115 76
            $orientatedItem->getItem(),
116
            $x,
117
            $y,
118
            $z,
119 76
            $orientatedItem->getWidth(),
120 76
            $orientatedItem->getLength(),
121 76
            $orientatedItem->getDepth()
122
        );
123
    }
124
125 2
    public function jsonSerialize(): array
126
    {
127
        return [
128 2
            'x' => $this->x,
129 2
            'y' => $this->y,
130 2
            'z' => $this->z,
131 2
            'width' => $this->width,
132 2
            'length' => $this->length,
133 2
            'depth' => $this->depth,
134
            'item' => [
135 2
                'description' => $this->item->getDescription(),
136 2
                'width' => $this->item->getWidth(),
137 2
                'length' => $this->item->getLength(),
138 2
                'depth' => $this->item->getDepth(),
139 2
                'allowedRotations' => $this->item->getAllowedRotations(),
140
            ],
141
        ];
142
    }
143
}
144