Completed
Push — 1.x-dev ( f03c49...17b59f )
by Doug
68:27 queued 64:38
created

OrientatedItem::isStable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 1
b 0
f 0
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
     * Constructor.
50
     *
51
     * @param Item $item
52
     * @param int  $width
53
     * @param int  $length
54
     * @param int  $depth
55
     */
56 33
    public function __construct(Item $item, $width, $length, $depth)
57
    {
58 33
        $this->item = $item;
59 33
        $this->width = $width;
60 33
        $this->length = $length;
61 33
        $this->depth = $depth;
62 33
        $this->surfaceFootprint = $width * $length;
63 33
    }
64
65
    /**
66
     * Item.
67
     *
68
     * @return Item
69
     */
70 30
    public function getItem()
71
    {
72 30
        return $this->item;
73
    }
74
75
    /**
76
     * Item width in mm in it's packed orientation.
77
     *
78
     * @return int
79
     */
80 32
    public function getWidth()
81
    {
82 32
        return $this->width;
83
    }
84
85
    /**
86
     * Item length in mm in it's packed orientation.
87
     *
88
     * @return int
89
     */
90 32
    public function getLength()
91
    {
92 32
        return $this->length;
93
    }
94
95
    /**
96
     * Item depth in mm in it's packed orientation.
97
     *
98
     * @return int
99
     */
100 32
    public function getDepth()
101
    {
102 32
        return $this->depth;
103
    }
104
105
    /**
106
     * Calculate the surface footprint of the current orientation.
107
     *
108
     * @return int
109
     */
110
    public function getSurfaceFootprint()
111
    {
112
        return $this->surfaceFootprint;
113
    }
114
115
    /**
116
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
117
     *
118
     * N.B. Assumes equal weight distribution.
119
     *
120
     * @return bool
121
     */
122 32
    public function isStable()
123
    {
124 32
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
125
126 32
        if (!isset(static::$stabilityCache[$cacheKey])) {
127 27
            static::$stabilityCache[$cacheKey] = (atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
128
        }
129
130 32
         return static::$stabilityCache[$cacheKey];
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1
    public function jsonSerialize()
137
    {
138
        return [
139 1
            'item' => $this->item,
140 1
            'width' => $this->width,
141 1
            'length' => $this->length,
142 1
            'depth' => $this->depth,
143
        ];
144
    }
145
146
    /**
147
     * @return string
148
     */
149 1
    public function __toString()
150
    {
151 1
        return $this->width . '|' . $this->length . '|' . $this->depth;
152
    }
153
}
154