| Total Complexity | 6 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 20 | class BoxList implements IteratorAggregate |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var Box[] |
||
| 24 | */ |
||
| 25 | private array $list = []; |
||
| 26 | |||
| 27 | private bool $isSorted = false; |
||
| 28 | |||
| 29 | 55 | public function __construct(private readonly BoxSorter $sorter = new DefaultBoxSorter()) |
|
| 30 | { |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Do a bulk create. |
||
| 35 | * |
||
| 36 | * @param Box[] $boxes |
||
| 37 | */ |
||
| 38 | 2 | public static function fromArray(array $boxes, bool $preSorted = false): self |
|
| 39 | { |
||
| 40 | 2 | $list = new self(); |
|
| 41 | 2 | $list->list = $boxes; |
|
| 42 | 2 | $list->isSorted = $preSorted; |
|
| 43 | |||
| 44 | 2 | return $list; |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return Traversable<Box> |
||
| 49 | */ |
||
| 50 | 54 | public function getIterator(): Traversable |
|
| 51 | { |
||
| 52 | 54 | if (!$this->isSorted) { |
|
| 53 | 53 | usort($this->list, $this->sorter->compare(...)); |
|
|
|
|||
| 54 | 53 | $this->isSorted = true; |
|
| 55 | } |
||
| 56 | |||
| 57 | 54 | return new ArrayIterator($this->list); |
|
| 58 | } |
||
| 59 | |||
| 60 | 51 | public function insert(Box $item): void |
|
| 61 | { |
||
| 62 | 51 | $this->list[] = $item; |
|
| 63 | } |
||
| 64 | } |
||
| 65 |