Completed
Push — remove_logging ( 2ce4b7 )
by Doug
01:52
created

PackedBox::__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 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 6
crap 1
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
     * Remaining width inside box for another item
37
     * @var int
38
     */
39
    protected $remainingWidth;
40
41
    /**
42
     * Remaining length inside box for another item
43
     * @var int
44
     */
45
    protected $remainingLength;
46
47
    /**
48
     * Remaining depth inside box for another item
49
     * @var int
50
     */
51
    protected $remainingDepth;
52
53
    /**
54
     * Remaining weight inside box for another item
55
     * @var int
56
     */
57
    protected $remainingWeight;
58
59
    /**
60
     * Get box used
61
     * @return Box
62
     */
63 5
    public function getBox()
64
    {
65 5
        return $this->box;
66
    }
67
68
    /**
69
     * Get items packed
70
     * @return ItemList
71
     */
72 23
    public function getItems()
73
    {
74 23
        return $this->items;
75
    }
76
77
    /**
78
     * Get packed weight
79
     * @return int weight in grams
80
     */
81 6
    public function getWeight()
82
    {
83
84 6
        if (!is_null($this->weight)) {
85 4
            return $this->weight;
86
        }
87
88 6
        $this->weight = $this->box->getEmptyWeight();
89 6
        $items = clone $this->items;
90 6
        foreach ($items as $item) {
91 6
            $this->weight += $item->getWeight();
92 6
        }
93 6
        return $this->weight;
94
    }
95
96
    /**
97
     * Get remaining width inside box for another item
98
     * @return int
99
     */
100
    public function getRemainingWidth()
101
    {
102
        return $this->remainingWidth;
103
    }
104
105
    /**
106
     * Get remaining length inside box for another item
107
     * @return int
108
     */
109
    public function getRemainingLength()
110
    {
111
        return $this->remainingLength;
112
    }
113
114
    /**
115
     * Get remaining depth inside box for another item
116
     * @return int
117
     */
118
    public function getRemainingDepth()
119
    {
120
        return $this->remainingDepth;
121
    }
122
123
    /**
124
     * Get remaining weight inside box for another item
125
     * @return int
126
     */
127
    public function getRemainingWeight()
128
    {
129
        return $this->remainingWeight;
130
    }
131
132
    /**
133
     * Get volume utilisation of the packed box
134
     * @return float
135
     */
136
    public function getVolumeUtilisation()
137
    {
138
        $itemVolume = 0;
139
140
        /** @var Item $item */
141
        foreach (clone $this->items as $item) {
142
            $itemVolume += $item->getVolume();
143
        }
144
145
        return round($itemVolume / $this->box->getInnerVolume() * 100, 1);
146
    }
147
148
149
150
    /**
151
     * Constructor
152
     * @param Box      $box
153
     * @param ItemList $itemList
154
     * @param int      $remainingWidth
155
     * @param int      $remainingLength
156
     * @param int      $remainingDepth
157
     * @param int      $remainingWeight
158
     */
159 23
    public function __construct(Box $box, ItemList $itemList, $remainingWidth, $remainingLength, $remainingDepth, $remainingWeight)
160
    {
161 23
        $this->box = $box;
162 23
        $this->items = $itemList;
163 23
        $this->remainingWidth = $remainingWidth;
164 23
        $this->remainingLength = $remainingLength;
165 23
        $this->remainingDepth = $remainingDepth;
166 23
        $this->remainingWeight = $remainingWeight;
167 23
    }
168
}
169