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

OrientatedItem::isSameDimensions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
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