Passed
Push — 3.x ( 43a424...548f96 )
by Doug
08:16
created

PackedItem::jsonSerialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.0013

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 29
ccs 18
cts 19
cp 0.9474
crap 3.0013
rs 9.568
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 ReturnTypeWillChange;
13
14
use function array_merge;
15
use function is_array;
16
17
/**
18
 * A packed item.
19
 *
20
 * @author Doug Wright
21
 */
22
class PackedItem implements JsonSerializable
23
{
24
    /**
25
     * @var int
26
     */
27
    protected $x;
28
29
    /**
30
     * @var int
31
     */
32
    protected $y;
33
34
    /**
35
     * @var int
36
     */
37
    protected $z;
38
39
    /**
40
     * @var Item
41
     */
42
    protected $item;
43
44
    /**
45
     * @var int
46
     */
47
    protected $width;
48
49
    /**
50
     * @var int
51
     */
52
    protected $length;
53
54
    /**
55
     * @var int
56
     */
57
    protected $depth;
58
59
    /**
60
     * PackedItem constructor.
61
     */
62 86
    public function __construct(Item $item, int $x, int $y, int $z, int $width, int $length, int $depth)
63
    {
64 86
        $this->item = $item;
65 86
        $this->x = $x;
66 86
        $this->y = $y;
67 86
        $this->z = $z;
68 86
        $this->width = $width;
69 86
        $this->length = $length;
70 86
        $this->depth = $depth;
71
    }
72
73 84
    public function getX(): int
74
    {
75 84
        return $this->x;
76
    }
77
78 84
    public function getY(): int
79
    {
80 84
        return $this->y;
81
    }
82
83 84
    public function getZ(): int
84
    {
85 84
        return $this->z;
86
    }
87
88 84
    public function getItem(): Item
89
    {
90 84
        return $this->item;
91
    }
92
93 84
    public function getWidth(): int
94
    {
95 84
        return $this->width;
96
    }
97
98 84
    public function getLength(): int
99
    {
100 84
        return $this->length;
101
    }
102
103 84
    public function getDepth(): int
104
    {
105 84
        return $this->depth;
106
    }
107
108 84
    public function getVolume(): int
109
    {
110 84
        return $this->width * $this->length * $this->depth;
111
    }
112
113 83
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
114
    {
115 83
        return new static(
116 83
            $orientatedItem->getItem(),
117
            $x,
118
            $y,
119
            $z,
120 83
            $orientatedItem->getWidth(),
121 83
            $orientatedItem->getLength(),
122 83
            $orientatedItem->getDepth()
123
        );
124
    }
125
126
    /**
127
     * @deprecated
128
     */
129
    public function toOrientatedItem(): OrientatedItem
130
    {
131
        return new OrientatedItem($this->item, $this->width, $this->length, $this->depth);
132
    }
133
134 1
    #[ReturnTypeWillChange]
135
    public function jsonSerialize()/* : mixed */
136
    {
137 1
        $userValues = [];
138
139 1
        if ($this->item instanceof JsonSerializable) {
140 1
            $userSerialisation = $this->item->jsonSerialize();
141 1
            if (is_array($userSerialisation)) {
142 1
                $userValues = $userSerialisation;
143
            } else {
144
                $userValues = ['extra' => $userSerialisation];
145
            }
146
        }
147
148
        return [
149 1
            'x' => $this->x,
150 1
            'y' => $this->y,
151 1
            'z' => $this->z,
152 1
            'width' => $this->width,
153 1
            'length' => $this->length,
154 1
            'depth' => $this->depth,
155 1
            'item' => array_merge(
156
                $userValues,
157
                [
158 1
                    'description' => $this->item->getDescription(),
159 1
                    'width' => $this->item->getWidth(),
160 1
                    'length' => $this->item->getLength(),
161 1
                    'depth' => $this->item->getDepth(),
162 1
                    'keepFlat' => $this->item->getKeepFlat(),
163
                ]
164
            ),
165
        ];
166
    }
167
}
168