Completed
Push — master ( 9610e0...320eee )
by Doug
08:43
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 ItemList
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 6
    public function getBox()
40
    {
41 6
        return $this->box;
42
    }
43
44
    /**
45
     * Get items packed
46
     * @return ItemList
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
     * Get volume utilisation of the packed box
158
     * @return float
159
     */
160 1
    public function getVolumeUtilisation()
161
    {
162 1
        $itemVolume = 0;
163
164
        /** @var PackedItem $item */
165 1
        foreach (clone $this->items as $item) {
166 1
            $itemVolume += $item->getItem()->getVolume();
167
        }
168
169 1
        return round($itemVolume / $this->box->getInnerVolume() * 100, 1);
170
    }
171
172
173
    /**
174
     * Constructor
175
     *
176
     * @param Box      $box
177
     * @param PackedItemList $itemList
178
     */
179 38
    public function __construct(Box $box, PackedItemList $itemList)
180
    {
181 38
        $this->box = $box;
182 38
        $this->items = $itemList;
0 ignored issues
show
Documentation Bug introduced by Doug Wright
It seems like $itemList of type object<DVDoug\BoxPacker\PackedItemList> is incompatible with the declared type object<DVDoug\BoxPacker\ItemList> of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
183
    }
184
}
185