Test Failed
Push — 2.x-dev ( 5e90af...eccc16 )
by Doug
03:16
created

OrientatedItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
namespace DVDoug\BoxPacker;
8
9
use JsonSerializable;
10
11
/**
12
 * An item to be packed.
13
 *
14
 * @author Doug Wright
15
 */
16
class OrientatedItem implements JsonSerializable
17
{
18
    /**
19
     * @var Item
20
     */
21
    protected $item;
22
23
    /**
24
     * @var int
25
     */
26
    protected $width;
27
28
    /**
29
     * @var int
30
     */
31
    protected $length;
32
33
    /**
34
     * @var int
35
     */
36
    protected $depth;
37
38
    /**
39
     * @var int
40
     */
41
    protected $surfaceFootprint;
42
43
    /**
44
     * @var bool[]
45
     */
46
    protected static $stabilityCache = [];
47
48
    /**
49
     * @var array
50
     */
51
    protected $dimensionsAsArray;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param Item $item
57
     * @param int  $width
58
     * @param int  $length
59
     * @param int  $depth
60
     */
61 29
    public function __construct(Item $item, $width, $length, $depth)
62
    {
63 29
        $this->item = $item;
64 29
        $this->width = $width;
65 29
        $this->length = $length;
66 29
        $this->depth = $depth;
67 29
        $this->surfaceFootprint = $width * $length;
68
69 29
        $this->dimensionsAsArray = [$width, $length, $depth];
70 29
        sort($this->dimensionsAsArray);
71 29
    }
72
73
    /**
74
     * Item.
75
     *
76
     * @return Item
77
     */
78 18
    public function getItem()
79
    {
80 18
        return $this->item;
81
    }
82
83
    /**
84
     * Item width in mm in it's packed orientation.
85
     *
86
     * @return int
87
     */
88 28
    public function getWidth()
89
    {
90 28
        return $this->width;
91
    }
92
93
    /**
94
     * Item length in mm in it's packed orientation.
95
     *
96
     * @return int
97
     */
98 28
    public function getLength()
99
    {
100 28
        return $this->length;
101
    }
102
103
    /**
104
     * Item depth in mm in it's packed orientation.
105
     *
106
     * @return int
107
     */
108 28
    public function getDepth()
109
    {
110 28
        return $this->depth;
111
    }
112
113
    /**
114
     * Calculate the surface footprint of the current orientation.
115
     *
116
     * @return int
117
     */
118 6
    public function getSurfaceFootprint()
119
    {
120 6
        return $this->surfaceFootprint;
121
    }
122
123
    /**
124
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
125
     *
126
     * N.B. Assumes equal weight distribution.
127
     *
128
     * @return bool
129
     */
130 28
    public function isStable()
131
    {
132 28
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
133
134 28
        if (!isset(static::$stabilityCache[$cacheKey])) {
135 25
            static::$stabilityCache[$cacheKey] = (atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
136
        }
137
138 28
         return static::$stabilityCache[$cacheKey];
139
    }
140
141
    /**
142
     * Is the supplied item the same size as this one?
143
     *
144
     * @internal
145
     */
146 26
    public function isSameDimensions(Item $item)
147
    {
148 26
        if ($item === $this->item) {
149 12
            return true;
150
        }
151
152 18
        $itemDimensions = [$item->getWidth(), $item->getLength(), $item->getDepth()];
153 18
        sort($itemDimensions);
154
155 18
        return $this->dimensionsAsArray === $itemDimensions;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function jsonSerialize()
162
    {
163
        return [
164 1
            'item' => $this->item,
165 1
            'width' => $this->width,
166 1
            'length' => $this->length,
167 1
            'depth' => $this->depth,
168
        ];
169
    }
170
171
    /**
172
     * @return string
173
     */
174 1
    public function __toString()
175
    {
176 1
        return $this->width . '|' . $this->length . '|' . $this->depth;
177
    }
178
}
179