Passed
Push — master ( 11fd0d...57f32f )
by Doug
02:39 queued 11s
created

PackedItemList::getVolume()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
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 function array_map;
12
use ArrayIterator;
13
use function count;
14
use Countable;
15
use IteratorAggregate;
16
use Traversable;
17
use function usort;
18
19
/**
20
 * List of packed items, ordered by volume.
21
 *
22
 * @author Doug Wright
23
 */
24
class PackedItemList implements Countable, IteratorAggregate
25
{
26
    /**
27
     * List containing items.
28
     *
29
     * @var PackedItem[]
30
     */
31
    private $list = [];
32
33
    /**
34
     * Has this list already been sorted?
35
     *
36
     * @var bool
37
     */
38
    private $isSorted = false;
39
40 64
    public function insert(PackedItem $item): void
41
    {
42 64
        $this->list[] = $item;
43 64
    }
44
45
    /**
46
     * @return Traversable<PackedItem>
47
     */
48 64
    public function getIterator(): Traversable
49
    {
50 64
        if (!$this->isSorted) {
51 64
            usort($this->list, [$this, 'compare']);
52 64
            $this->isSorted = true;
53
        }
54
55 64
        return new ArrayIterator($this->list);
56
    }
57
58
    /**
59
     * Number of items in list.
60
     */
61 64
    public function count(): int
62
    {
63 64
        return count($this->list);
64
    }
65
66
    /**
67
     * Get copy of this list as a standard PHP array.
68
     *
69
     * @internal
70
     *
71
     * @return Item[]
72
     */
73 8
    public function asItemArray(): array
74
    {
75 8
        return array_map(function (PackedItem $packedItem) {
76 8
            return $packedItem->getItem();
77 8
        }, $this->list);
78
    }
79
80
    /**
81
     * Get total volume of these items.
82
     */
83 64
    public function getVolume(): int
84
    {
85 64
        $volume = 0;
86
87 64
        foreach ($this->list as $item) {
88 64
            $volume += $item->getVolume();
89
        }
90
91 64
        return $volume;
92
    }
93
94 56
    private function compare(PackedItem $itemA, PackedItem $itemB): int
95
    {
96 56
        $itemAVolume = $itemA->getItem()->getWidth() * $itemA->getItem()->getLength() * $itemA->getItem()->getDepth();
97 56
        $itemBVolume = $itemB->getItem()->getWidth() * $itemB->getItem()->getLength() * $itemB->getItem()->getDepth();
98
99 56
        return ($itemBVolume <=> $itemAVolume) ?: ($itemB->getItem()->getWeight() <=> $itemA->getItem()->getWeight());
100
    }
101
}
102