Skip to content

Commit

Permalink
Take the idea from #198, but don't duplicate the method
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Apr 18, 2020
1 parent 40661ee commit cdd458b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/Packer.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function pack(): PackedBoxList
*
* @throws NoBoxesAvailableException
*/
public function doVolumePacking(bool $singlePassMode = false): PackedBoxList
public function doVolumePacking(bool $singlePassMode = false, bool $enforceSingleBox = false): PackedBoxList
{
$packedBoxes = new PackedBoxList();

Expand All @@ -171,7 +171,7 @@ public function doVolumePacking(bool $singlePassMode = false): PackedBoxList
$packedBoxesIteration = [];

//Loop through boxes starting with smallest, see what happens
foreach ($this->getBoxList() as $box) {
foreach ($this->getBoxList($enforceSingleBox) as $box) {
if ($this->boxesQtyAvailable[$box] > 0) {
$volumePacker = new VolumePacker($box, $this->items);
$volumePacker->setLogger($this->logger);
Expand All @@ -188,8 +188,15 @@ public function doVolumePacking(bool $singlePassMode = false): PackedBoxList
}
}

//Find best box of iteration, and remove packed items from unpacked list
$bestBox = $this->findBestBoxFromIteration($packedBoxesIteration);
try {
//Find best box of iteration, and remove packed items from unpacked list
$bestBox = $this->findBestBoxFromIteration($packedBoxesIteration);
} catch (NoBoxesAvailableException $e) {
if ($enforceSingleBox) {
return new PackedBoxList();
}
throw $e;
}

/** @var PackedItem $packedItem */
foreach ($bestBox->getItems() as $packedItem) {
Expand All @@ -208,7 +215,7 @@ public function doVolumePacking(bool $singlePassMode = false): PackedBoxList
* so that the smallest boxes are evaluated first, but this means that time is spent on boxes that cannot possibly
* hold the entire set of items due to volume limitations. These should be evaluated first.
*/
protected function getBoxList(): iterable
protected function getBoxList(bool $enforceSingleBox = false): iterable
{
$itemVolume = 0;
/** @var Item $item */
Expand All @@ -222,7 +229,7 @@ protected function getBoxList(): iterable
foreach ($this->boxes as $box) {
if ($box->getInnerWidth() * $box->getInnerLength() * $box->getInnerDepth() >= $itemVolume) {
$preferredBoxes[] = $box;
} else {
} elseif (!$enforceSingleBox) {
$otherBoxes[] = $box;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/WeightRedistributor.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private function doVolumeRepack(iterable $items, Box $currentBox): PackedBoxList
$packer->setBoxQuantity($currentBox, max(PHP_INT_MAX, $this->boxesQtyAvailable[$currentBox] + 1));
$packer->setItems($items);

return $packer->doVolumePacking(true);
return $packer->doVolumePacking(true, true);
}

/**
Expand Down

0 comments on commit cdd458b

Please sign in to comment.