Passed
Push — 3.x ( 922c95...5b6501 )
by Doug
12:25 queued 11:05
created

ItemList::hasConstrainedItems()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 4
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_key_last;
12
use function array_pop;
13
use function array_reverse;
14
use function array_slice;
15
use ArrayIterator;
16
use function count;
17
use Countable;
18
use function current;
19
use function end;
20
use IteratorAggregate;
21
use function key;
22
use const PHP_VERSION_ID;
23
use function prev;
24
use Traversable;
25
use function usort;
26
27
/**
28
 * List of items to be packed, ordered by volume.
29
 *
30
 * @author Doug Wright
31
 */
32
class ItemList implements Countable, IteratorAggregate
33
{
34
    /**
35
     * List containing items.
36
     *
37
     * @var Item[]
38
     */
39
    private $list = [];
40
41
    /**
42
     * Has this list already been sorted?
43
     *
44
     * @var bool
45
     */
46
    private $isSorted = false;
47
48
    /**
49
     * Does this list contain constrained items?
50
     *
51
     * @var bool
52
     */
53
    private $hasConstrainedItems;
54
55
    /**
56
     * Do a bulk create.
57
     *
58
     * @param  Item[]   $items
59
     * @return ItemList
60
     */
61 73
    public static function fromArray(array $items, bool $preSorted = false): self
62
    {
63 73
        $list = new static();
64 73
        $list->list = array_reverse($items); // internal sort is largest at the end
65 73
        $list->isSorted = $preSorted;
66
67 73
        return $list;
68
    }
69
70 82
    public function insert(Item $item): void
71
    {
72 82
        $this->list[] = $item;
73 82
        $this->isSorted = false;
74 82
        $this->hasConstrainedItems = $this->hasConstrainedItems || $item instanceof ConstrainedPlacementItem;
75 82
    }
76
77
    /**
78
     * Remove item from list.
79
     */
80 2
    public function remove(Item $item): void
81
    {
82 2
        if (!$this->isSorted) {
83 2
            usort($this->list, [$this, 'compare']);
84 2
            $this->isSorted = true;
85
        }
86
87 2
        end($this->list);
88
        do {
89 2
            if (current($this->list) === $item) {
90 2
                unset($this->list[key($this->list)]);
91
92 2
                return;
93
            }
94
        } while (prev($this->list) !== false);
95
    }
96
97 53
    public function removePackedItems(PackedItemList $packedItemList): void
98
    {
99 53
        foreach ($packedItemList as $packedItem) {
100 48
            end($this->list);
101
            do {
102 48
                if (current($this->list) === $packedItem->getItem()) {
103 48
                    unset($this->list[key($this->list)]);
104
105 48
                    break;
106
                }
107 7
            } while (prev($this->list) !== false);
108
        }
109 53
    }
110
111
    /**
112
     * @internal
113
     */
114 75
    public function extract(): Item
115
    {
116 75
        if (!$this->isSorted) {
117 45
            usort($this->list, [$this, 'compare']);
118 45
            $this->isSorted = true;
119
        }
120
121 75
        return array_pop($this->list);
122
    }
123
124
    /**
125
     * @internal
126
     */
127 63
    public function top(): Item
128
    {
129 63
        if (!$this->isSorted) {
130 1
            usort($this->list, [$this, 'compare']);
131 1
            $this->isSorted = true;
132
        }
133
134 63
        if (PHP_VERSION_ID < 70300) {
135
            return array_slice($this->list, -1, 1)[0];
136
        }
137
138 63
        return $this->list[array_key_last($this->list)];
139
    }
140
141
    /**
142
     * @internal
143
     * @return ItemList
144
     */
145 59
    public function topN(int $n): self
146
    {
147 59
        if (!$this->isSorted) {
148 1
            usort($this->list, [$this, 'compare']);
149 1
            $this->isSorted = true;
150
        }
151
152 59
        $topNList = new self();
153 59
        $topNList->list = array_slice($this->list, -$n, $n);
154 59
        $topNList->isSorted = true;
155
156 59
        return $topNList;
157
    }
158
159
    /**
160
     * @return Traversable|Item[]
161
     */
162 78
    public function getIterator(): Traversable
163
    {
164 78
        if (!$this->isSorted) {
165 34
            usort($this->list, [$this, 'compare']);
166 34
            $this->isSorted = true;
167
        }
168
169 78
        return new ArrayIterator(array_reverse($this->list));
170
    }
171
172
    /**
173
     * Number of items in list.
174
     */
175 78
    public function count(): int
176
    {
177 78
        return count($this->list);
178
    }
179
180
    /**
181
     * Does this list contain items with constrained placement criteria.
182
     */
183 73
    public function hasConstrainedItems(): bool
184
    {
185 73
        if (!isset($this->hasConstrainedItems)) {
186 39
            $this->hasConstrainedItems = false;
187 39
            foreach ($this->list as $item) {
188 39
                if ($item instanceof ConstrainedPlacementItem) {
189 2
                    $this->hasConstrainedItems = true;
190 2
                    break;
191
                }
192
            }
193
        }
194
195 73
        return $this->hasConstrainedItems;
196
    }
197
198 76
    private static function compare(Item $itemA, Item $itemB): int
199
    {
200 76
        $volumeDecider = $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth() <=> $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth();
201 76
        if ($volumeDecider !== 0) {
202 40
            return $volumeDecider;
203
        }
204 67
        $weightDecider = $itemA->getWeight() - $itemB->getWeight();
205 67
        if ($weightDecider !== 0) {
206 5
            return $weightDecider;
207
        }
208
209 62
        return $itemB->getDescription() <=> $itemA->getDescription();
210
    }
211
}
212