Completed
Push — master ( 6f5502...4734ca )
by Doug
17:20 queued 03:31
created

src/PackedBox.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem)
4
 * @package BoxPacker
5
 * @author Doug Wright
6
 */
7
namespace DVDoug\BoxPacker;
8
9
/**
10
 * A "box" with items
11
 * @author Doug Wright
12
 * @package BoxPacker
13
 */
14
class PackedBox
15
{
16
17
    /**
18
     * Box used
19
     * @var Box
20
     */
21
    protected $box;
22
23
    /**
24
     * Items in the box
25
     * @var PackedItemList
26
     */
27
    protected $items;
28
29
    /**
30
     * Total weight of box
31
     * @var int
32
     */
33
    protected $weight;
34
35
    /**
36
     * Get box used
37
     * @return Box
38
     */
39 4
    public function getBox()
40
    {
41 4
        return $this->box;
42
    }
43
44
    /**
45
     * Get items packed
46
     * @return PackedItemList
47
     */
48 35
    public function getItems()
49
    {
50 35
        return $this->items;
51
    }
52
53
    /**
54
     * Get packed weight
55
     * @return int weight in grams
56
     */
57 9
    public function getWeight()
58
    {
59 9
        if (!is_null($this->weight)) {
60 6
            return $this->weight;
61
        }
62
63 9
        $this->weight = $this->box->getEmptyWeight();
64 9
        $items = clone $this->items;
65
        /** @var PackedItem $item */
66 9
        foreach ($items as $item) {
67 9
            $this->weight += $item->getItem()->getWeight();
68
        }
69 9
        return $this->weight;
70
    }
71
72
    /**
73
     * Get remaining width inside box for another item
74
     * @return int
75
     */
76 1
    public function getRemainingWidth()
77
    {
78 1
        return $this->box->getInnerWidth() - $this->getUsedWidth();
79
    }
80
81
    /**
82
     * Get remaining length inside box for another item
83
     * @return int
84
     */
85 1
    public function getRemainingLength()
86
    {
87 1
        return $this->box->getInnerLength() - $this->getUsedLength();
88
    }
89
90
    /**
91
     * Get remaining depth inside box for another item
92
     * @return int
93
     */
94 1
    public function getRemainingDepth()
95
    {
96 1
        return $this->box->getInnerDepth() - $this->getUsedDepth();
97
    }
98
99
    /**
100
     * Used width inside box for packing items
101
     * @return int
102
     */
103 5
    public function getUsedWidth()
104
    {
105 5
        $maxWidth = 0;
106
107
        /** @var PackedItem $item */
108 5
        foreach (clone $this->items as $item) {
109 5
            $maxWidth = max($maxWidth, $item->getX() + $item->getWidth());
110
        }
111
112 5
        return $maxWidth;
113
    }
114
115
    /**
116
     * Used length inside box for packing items
117
     * @return int
118
     */
119 5
    public function getUsedLength()
120
    {
121 5
        $maxLength = 0;
122
123
        /** @var PackedItem $item */
124 5
        foreach (clone $this->items as $item) {
125 5
            $maxLength = max($maxLength, $item->getY() + $item->getLength());
126
        }
127
128 5
        return $maxLength;
129
    }
130
131
    /**
132
     * Used depth inside box for packing items
133
     * @return int
134
     */
135 5
    public function getUsedDepth()
136
    {
137 5
        $maxDepth = 0;
138
139
        /** @var PackedItem $item */
140 5
        foreach (clone $this->items as $item) {
141 5
            $maxDepth = max($maxDepth, $item->getZ() + $item->getDepth());
142
        }
143
144 5
        return $maxDepth;
145
    }
146
147
    /**
148
     * Get remaining weight inside box for another item
149
     * @return int
150
     */
151 1
    public function getRemainingWeight()
152
    {
153 1
        return $this->box->getMaxWeight() - $this->getWeight();
154
    }
155
156
    /**
157
     * @return int
158
     */
159 5
    public function getInnerVolume()
160
    {
161 5
        return $this->box->getInnerDepth() * $this->box->getInnerDepth() * $this->box->getInnerDepth();
162
    }
163
164
    /**
165
     * Get volume utilisation of the packed box
166
     * @return float
167
     */
168 1
    public function getVolumeUtilisation()
169
    {
170 1
        $itemVolume = 0;
171
172
        /** @var PackedItem $item */
173 1 View Code Duplication
        foreach (clone $this->items as $item) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174 1
            $itemVolume += ($item->getItem()->getWidth() * $item->getItem()->getLength() * $item->getItem()->getDepth());
175
        }
176
177 1
        return round($itemVolume / $this->getInnerVolume() * 100, 1);
178
    }
179
180
181
    /**
182
     * Constructor
183
     *
184
     * @param Box      $box
185
     * @param PackedItemList $itemList
186
     */
187 38
    public function __construct(Box $box, PackedItemList $itemList)
188
    {
189 38
        $this->box = $box;
190 38
        $this->items = $itemList;
191
    }
192
}
193