Passed
Push — 3.x ( f78397...42dd24 )
by Doug
09:29
created

PackedItem   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 6
Bugs 3 Features 0
Metric Value
eloc 46
c 6
b 3
f 0
dl 0
loc 131
ccs 42
cts 44
cp 0.9545
rs 10
wmc 12

12 Methods

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