Passed
Push — 3.x ( 688d36...175f8a )
by Doug
06:40
created

PackedItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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