Passed
Push — 1.x ( 0e8de8...457fa0 )
by Doug
30:53
created

OrientatedItem::getLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
cc 1
eloc 1
c 3
b 3
f 0
nc 1
nop 0
dl 0
loc 3
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
     * Constructor.
50
     *
51
     * @param Item $item
52
     * @param int  $width
53
     * @param int  $length
54
     * @param int  $depth
55
     */
56
    public function __construct(Item $item, $width, $length, $depth)
57
    {
58
        $this->item = $item;
59
        $this->width = $width;
60
        $this->length = $length;
61
        $this->depth = $depth;
62
        $this->surfaceFootprint = $width * $length;
63
    }
64
65
    /**
66
     * Item.
67
     *
68
     * @return Item
69
     */
70
    public function getItem()
71
    {
72
        return $this->item;
73
    }
74
75
    /**
76
     * Item width in mm in it's packed orientation.
77
     *
78
     * @return int
79
     */
80
    public function getWidth()
81
    {
82
        return $this->width;
83
    }
84
85
    /**
86
     * Item length in mm in it's packed orientation.
87
     *
88
     * @return int
89
     */
90
    public function getLength()
91
    {
92
        return $this->length;
93
    }
94
95
    /**
96
     * Item depth in mm in it's packed orientation.
97
     *
98
     * @return int
99
     */
100
    public function getDepth()
101
    {
102
        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
    public function isStable()
123
    {
124
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
125
126
        if (!isset(static::$stabilityCache[$cacheKey])) {
127
            static::$stabilityCache[$cacheKey] = (atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
128
        }
129
130
         return static::$stabilityCache[$cacheKey];
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function jsonSerialize()
137
    {
138
        return [
139
            'item' => $this->item,
140
            'width' => $this->width,
141
            'length' => $this->length,
142
            'depth' => $this->depth,
143
        ];
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function __toString()
150
    {
151
        return $this->width . '|' . $this->length . '|' . $this->depth;
152
    }
153
}
154