Passed
Push — master ( 73ddcb...629397 )
by Doug
02:34
created

OrientatedItem   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 3 Features 0
Metric Value
wmc 12
eloc 32
c 7
b 3
f 0
dl 0
loc 137
ccs 35
cts 35
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getLength() 0 3 1
A getDepth() 0 3 1
A getWidth() 0 3 1
A __construct() 0 10 1
A getSurfaceFootprint() 0 3 1
A isStable() 0 5 2
A getItem() 0 3 1
A jsonSerialize() 0 7 1
A isSameDimensions() 0 10 2
A __toString() 0 3 1
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
     * @var array
53
     */
54
    protected $dimensionsAsArray;
55
56
    /**
57
     * Constructor.
58
     */
59 77
    public function __construct(Item $item, int $width, int $length, int $depth)
60
    {
61 77
        $this->item = $item;
62 77
        $this->width = $width;
63 77
        $this->length = $length;
64 77
        $this->depth = $depth;
65 77
        $this->surfaceFootprint = $width * $length;
66
67 77
        $this->dimensionsAsArray = [$width, $length, $depth];
68 77
        sort($this->dimensionsAsArray);
69 77
    }
70
71
    /**
72
     * Item.
73
     */
74 73
    public function getItem(): Item
75
    {
76 73
        return $this->item;
77
    }
78
79
    /**
80
     * Item width in mm in it's packed orientation.
81
     */
82 76
    public function getWidth(): int
83
    {
84 76
        return $this->width;
85
    }
86
87
    /**
88
     * Item length in mm in it's packed orientation.
89
     */
90 76
    public function getLength(): int
91
    {
92 76
        return $this->length;
93
    }
94
95
    /**
96
     * Item depth in mm in it's packed orientation.
97
     */
98 76
    public function getDepth(): int
99
    {
100 76
        return $this->depth;
101
    }
102
103
    /**
104
     * Calculate the surface footprint of the current orientation.
105
     */
106 54
    public function getSurfaceFootprint(): int
107
    {
108 54
        return $this->surfaceFootprint;
109
    }
110
111
    /**
112
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
113
     *
114
     * N.B. Assumes equal weight distribution.
115
     */
116 73
    public function isStable(): bool
117
    {
118 73
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
119
120 73
        return static::$stabilityCache[$cacheKey] ?? (static::$stabilityCache[$cacheKey] = atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
121
    }
122
123
    /**
124
     * Is the supplied item the same size as this one?
125
     *
126
     * @internal
127
     */
128 70
    public function isSameDimensions(Item $item): bool
129
    {
130 70
        if ($item === $this->item) {
131 46
            return true;
132
        }
133
134 43
        $itemDimensions = [$item->getWidth(), $item->getLength(), $item->getDepth()];
135 43
        sort($itemDimensions);
136
137 43
        return $this->dimensionsAsArray === $itemDimensions;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 1
    public function jsonSerialize()
144
    {
145
        return [
146 1
            'item' => $this->item,
147 1
            'width' => $this->width,
148 1
            'length' => $this->length,
149 1
            'depth' => $this->depth,
150
        ];
151
    }
152
153 1
    public function __toString(): string
154
    {
155 1
        return $this->width . '|' . $this->length . '|' . $this->depth;
156
    }
157
}
158