public function pack() { $this->_sort(); $items = $this->items; while (!empty($items)) { $level = new Level(count($this->levels), $this->maxLength); foreach ($items as $i => $item) { if ($level->getRemainFloorLength() >= $item->getLength()) { $level->addFloor(new LevelItem($item)); unset($items[$i]); } } foreach ($items as $i => $item) { if ($level->isItemCeilPlaced($item)) { $level->addCeil(new LevelItem($item)); unset($items[$i]); } } $this->levels[] = $level; } return $this; }