Passed
Push — master ( 7c9e12...bc0d5e )
by Doug
01:34
created

ItemList   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 146
ccs 46
cts 48
cp 0.9583
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A top() 0 9 2
A getIterator() 0 8 2
A compare() 0 14 3
A topN() 0 11 2
A count() 0 3 1
A remove() 0 6 3
A insert() 0 3 1
A insertFromArray() 0 4 2
A extract() 0 8 2
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 ArrayIterator;
12
use Countable;
13
use IteratorAggregate;
14
use Traversable;
15
use function array_pop;
16
use function array_reverse;
17
use function array_slice;
18
use function count;
19
use function end;
20
use function usort;
21
22
/**
23
 * List of items to be packed, ordered by volume.
24
 *
25
 * @author Doug Wright
26
 */
27
class ItemList implements Countable, IteratorAggregate
28
{
29
    /**
30
     * List containing items.
31
     *
32
     * @var Item[]
33
     */
34
    private $list = [];
35
36
    /**
37
     * Has this list already been sorted?
38
     *
39
     * @var bool
40
     */
41
    private $isSorted = false;
42
43
    /**
44
     * @param Item $item
45
     */
46 27
    public function insert(Item $item): void
47
    {
48 27
        $this->list[] = $item;
49 27
    }
50
51
    /**
52
     * Do a bulk insert.
53
     *
54
     * @internal
55
     *
56
     * @param Item[] $items
57
     */
58 13
    public function insertFromArray(array $items): void
59
    {
60 13
        foreach ($items as $item) {
61 13
            $this->insert($item);
62
        }
63 13
    }
64
65
    /**
66
     * Remove item from list.
67
     *
68
     * @param Item $item
69
     */
70 7
    public function remove(Item $item): void
71
    {
72 7
        foreach ($this->list as $key => $itemToCheck) {
73 7
            if ($itemToCheck === $item) {
74 7
                unset($this->list[$key]);
75 7
                break;
76
            }
77
        }
78 7
    }
79
80
    /**
81
     * @internal
82
     *
83
     * @return Item
84
     */
85 23
    public function extract(): Item
86
    {
87 23
        if (!$this->isSorted) {
88 23
            usort($this->list, [$this, 'compare']);
89 23
            $this->isSorted = true;
90
        }
91
92 23
        return array_pop($this->list);
93
    }
94
95
    /**
96
     * @internal
97
     *
98
     * @return Item
99
     */
100 22
    public function top(): Item
101
    {
102 22
        if (!$this->isSorted) {
103 3
            usort($this->list, [$this, 'compare']);
104 3
            $this->isSorted = true;
105
        }
106 22
        $temp = $this->list;
107
108 22
        return end($temp);
109
    }
110
111
    /**
112
     * @internal
113
     *
114
     * @param  int      $n
115
     * @return ItemList
116
     */
117 13
    public function topN(int $n): self
118
    {
119 13
        if (!$this->isSorted) {
120
            usort($this->list, [$this, 'compare']);
121
            $this->isSorted = true;
122
        }
123
124 13
        $topNList = new self();
125 13
        $topNList->insertFromArray(array_slice($this->list, 0, $n));
126
127 13
        return $topNList;
128
    }
129
130
    /**
131
     * @return Traversable
132
     */
133 4
    public function getIterator(): Traversable
134
    {
135 4
        if (!$this->isSorted) {
136 4
            usort($this->list, [$this, 'compare']);
137 4
            $this->isSorted = true;
138
        }
139
140 4
        return new ArrayIterator(array_reverse($this->list));
141
    }
142
143
    /**
144
     * Number of items in list.
145
     *
146
     * @return int
147
     */
148 25
    public function count(): int
149
    {
150 25
        return count($this->list);
151
    }
152
153
    /**
154
     * @param Item $itemA
155
     * @param Item $itemB
156
     *
157
     * @return int
158
     */
159 23
    private function compare(Item $itemA, Item $itemB): int
160
    {
161 23
        $itemAVolume = $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth();
162 23
        $itemBVolume = $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth();
163 23
        $volumeDecider = $itemAVolume <=> $itemBVolume;
164 23
        $weightDecider = $itemA->getWeight() - $itemB->getWeight();
165 23
        if ($volumeDecider !== 0) {
166 13
            return $volumeDecider;
167
        }
168 21
        if ($weightDecider !== 0) {
169 3
            return $weightDecider;
170
        }
171
172 18
        return $itemB->getDescription() <=> $itemA->getDescription();
173
    }
174
}
175