| Total Complexity | 7 |
| Total Lines | 72 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 21 | class BoxList implements IteratorAggregate |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * List containing boxes. |
||
| 25 | * |
||
| 26 | * @var Box[] |
||
| 27 | */ |
||
| 28 | private $list = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Has this list already been sorted? |
||
| 32 | * |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | private $isSorted = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Do a bulk create. |
||
| 39 | * |
||
| 40 | 32 | * @param Box[] $boxes |
|
| 41 | * |
||
| 42 | 32 | * @return BoxList |
|
| 43 | 32 | */ |
|
| 44 | 32 | public static function fromArray(array $boxes, bool $preSorted = false): self |
|
| 51 | } |
||
| 52 | 31 | ||
| 53 | 31 | /** |
|
| 54 | * @return Traversable|Box[] |
||
| 55 | */ |
||
| 56 | public function getIterator(): Traversable |
||
| 57 | { |
||
| 58 | if (!$this->isSorted) { |
||
| 59 | 16 | usort($this->list, [$this, 'compare']); |
|
| 60 | $this->isSorted = true; |
||
| 61 | 16 | } |
|
| 62 | 16 | ||
| 63 | return new ArrayIterator($this->list); |
||
| 64 | 16 | } |
|
| 65 | |||
| 66 | 16 | public function insert(Box $item): void |
|
| 67 | 12 | { |
|
| 68 | $this->list[] = $item; |
||
| 69 | } |
||
| 70 | 4 | ||
| 71 | 4 | /** |
|
| 72 | 4 | * @param Box $boxA |
|
| 73 | * @param Box $boxB |
||
| 74 | */ |
||
| 75 | public static function compare($boxA, $boxB): int |
||
| 93 | } |
||
| 94 | } |
||
| 95 |