Passed
Pull Request — master (#344)
by
unknown
05:27 queued 03:46
created

PackedItem::getLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
13
/**
14
 * A packed item.
15
 */
16
class PackedItem implements JsonSerializable
17
{
18
    protected int $x;
19
20
    protected int $y;
21
22
    protected int $z;
23
24
    protected Item $item;
25
26
    protected int $width;
27
28
    protected int $length;
29
30
    protected int $depth;
31
32 82
    public function __construct(Item $item, int $x, int $y, int $z, int $width, int $length, int $depth)
33
    {
34 82
        $this->item = $item;
35 82
        $this->x = $x;
36 82
        $this->y = $y;
37 82
        $this->z = $z;
38 82
        $this->width = $width;
39 82
        $this->length = $length;
40 82
        $this->depth = $depth;
41
    }
42
43 82
    public function getX(): int
44
    {
45 82
        return $this->x;
46
    }
47
48 82
    public function getY(): int
49
    {
50 82
        return $this->y;
51
    }
52
53 82
    public function getZ(): int
54
    {
55 82
        return $this->z;
56
    }
57
58 82
    public function getItem(): Item
59
    {
60 82
        return $this->item;
61
    }
62
63 82
    public function getWidth(): int
64
    {
65 82
        return $this->width;
66
    }
67
68 82
    public function getLength(): int
69
    {
70 82
        return $this->length;
71
    }
72
73 82
    public function getDepth(): int
74
    {
75 82
        return $this->depth;
76
    }
77
78 82
    public function getVolume(): int
79
    {
80 82
        return $this->width * $this->length * $this->depth;
81
    }
82
83 82
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
84
    {
85 82
        return new self(
86 82
            $orientatedItem->getItem(),
87
            $x,
88
            $y,
89
            $z,
90 82
            $orientatedItem->getWidth(),
91 82
            $orientatedItem->getLength(),
92 82
            $orientatedItem->getDepth()
93
        );
94
    }
95
96
    public function jsonSerialize(): array
97
    {
98
        return [
99
            'x' => $this->x,
100
            'y' => $this->y,
101
            'z' => $this->z,
102
            'width' => $this->width,
103
            'length' => $this->length,
104
            'depth' => $this->depth,
105
            'item' => [
106
                'description' => $this->item->getDescription(),
107
                'width' => $this->item->getWidth(),
108
                'length' => $this->item->getLength(),
109
                'depth' => $this->item->getDepth(),
110
                'allowedRotation' => $this->item->getAllowedRotation(),
111
            ],
112
        ];
113
    }
114
}
115