Passed
Push — master ( efb060...599fbf )
by Doug
03:20
created

PackedBox::getUsedWidth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
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
/**
12
 * A "box" with items.
13
 *
14
 * @author Doug Wright
15
 */
16
class PackedBox
17
{
18
    /**
19
     * Box used.
20
     *
21
     * @var Box
22
     */
23
    protected $box;
24
25
    /**
26
     * Items in the box.
27
     *
28
     * @var PackedItemList
29
     */
30
    protected $items;
31
32
    /**
33
     * Total weight of box.
34
     *
35
     * @var int
36
     */
37
    protected $weight;
38
39
    /**
40
     * Get box used.
41
     *
42
     * @return Box
43
     */
44 6
    public function getBox(): Box
45
    {
46 6
        return $this->box;
47
    }
48
49
    /**
50
     * Get items packed.
51
     *
52
     * @return PackedItemList
53
     */
54 21
    public function getItems(): PackedItemList
55
    {
56 21
        return $this->items;
57
    }
58
59
    /**
60
     * Get packed weight.
61
     *
62
     * @return int weight in grams
63
     */
64 3
    public function getWeight(): int
65
    {
66 3
        if (!is_null($this->weight)) {
67 2
            return $this->weight;
68
        }
69
70 3
        $this->weight = $this->box->getEmptyWeight();
71
        /** @var PackedItem $item */
72 3
        foreach ($this->items as $item) {
73 3
            $this->weight += $item->getItem()->getWeight();
74
        }
75
76 3
        return $this->weight;
77
    }
78
79
    /**
80
     * Get remaining width inside box for another item.
81
     *
82
     * @return int
83
     */
84 1
    public function getRemainingWidth(): int
85
    {
86 1
        return $this->box->getInnerWidth() - $this->getUsedWidth();
87
    }
88
89
    /**
90
     * Get remaining length inside box for another item.
91
     *
92
     * @return int
93
     */
94 1
    public function getRemainingLength(): int
95
    {
96 1
        return $this->box->getInnerLength() - $this->getUsedLength();
97
    }
98
99
    /**
100
     * Get remaining depth inside box for another item.
101
     *
102
     * @return int
103
     */
104 1
    public function getRemainingDepth(): int
105
    {
106 1
        return $this->box->getInnerDepth() - $this->getUsedDepth();
107
    }
108
109
    /**
110
     * Used width inside box for packing items.
111
     *
112
     * @return int
113
     */
114 1
    public function getUsedWidth(): int
115
    {
116 1
        $maxWidth = 0;
117
118
        /** @var PackedItem $item */
119 1
        foreach ($this->items as $item) {
120 1
            $maxWidth = max($maxWidth, $item->getX() + $item->getWidth());
121
        }
122
123 1
        return $maxWidth;
124
    }
125
126
    /**
127
     * Used length inside box for packing items.
128
     *
129
     * @return int
130
     */
131 1
    public function getUsedLength(): int
132
    {
133 1
        $maxLength = 0;
134
135
        /** @var PackedItem $item */
136 1
        foreach ($this->items as $item) {
137 1
            $maxLength = max($maxLength, $item->getY() + $item->getLength());
138
        }
139
140 1
        return $maxLength;
141
    }
142
143
    /**
144
     * Used depth inside box for packing items.
145
     *
146
     * @return int
147
     */
148 1
    public function getUsedDepth(): int
149
    {
150 1
        $maxDepth = 0;
151
152
        /** @var PackedItem $item */
153 1
        foreach ($this->items as $item) {
154 1
            $maxDepth = max($maxDepth, $item->getZ() + $item->getDepth());
155
        }
156
157 1
        return $maxDepth;
158
    }
159
160
    /**
161
     * Get remaining weight inside box for another item.
162
     *
163
     * @return int
164
     */
165 1
    public function getRemainingWeight(): int
166
    {
167 1
        return $this->box->getMaxWeight() - $this->getWeight();
168
    }
169
170
    /**
171
     * @return int
172
     */
173 3
    public function getInnerVolume(): int
174
    {
175 3
        return $this->box->getInnerWidth() * $this->box->getInnerLength() * $this->box->getInnerDepth();
176
    }
177
178
    /**
179
     * Get used volume of the packed box.
180
     *
181
     * @return int
182
     */
183 1
    public function getUsedVolume(): int
184
    {
185 1
        $volume = 0;
186
187
        /** @var PackedItem $item */
188 1
        foreach ($this->items as $item) {
189 1
            $volume += ($item->getWidth() * $item->getLength() * $item->getDepth());
190
        }
191
192 1
        return $volume;
193
    }
194
195
    /**
196
     * Get unused volume of the packed box.
197
     *
198
     * @return int
199
     */
200 1
    public function getUnusedVolume(): int
201
    {
202 1
        return $this->getInnerVolume() - $this->getUsedVolume();
203
    }
204
205
    /**
206
     * Get volume utilisation of the packed box.
207
     *
208
     * @return float
209
     */
210 1
    public function getVolumeUtilisation(): float
211
    {
212 1
        return round($this->getUsedVolume() / $this->getInnerVolume() * 100, 1);
213
    }
214
215
    /**
216
     * Constructor.
217
     *
218
     * @param Box            $box
219
     * @param PackedItemList $packedItemList
220
     */
221 23
    public function __construct(Box $box, PackedItemList $packedItemList)
222
    {
223 23
        $this->box = $box;
224 23
        $this->items = $packedItemList;
225 23
    }
226
}
227