Passed
Push — master ( b3e52b...de1ca6 )
by Doug
11:09
created

OrientatedItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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