diff --git a/src/Packer.php b/src/Packer.php index ee26e7e..3e4737f 100644 --- a/src/Packer.php +++ b/src/Packer.php @@ -45,7 +45,7 @@ class Packer implements LoggerAwareInterface /** * Quantities available of each box type. */ - protected SplObjectStorage $boxesQtyAvailable; + protected SplObjectStorage $boxQuantitiesAvailable; protected PackedBoxSorter $packedBoxSorter; @@ -55,7 +55,7 @@ public function __construct() { $this->items = new ItemList(); $this->boxes = new BoxList(); - $this->boxesQtyAvailable = new SplObjectStorage(); + $this->boxQuantitiesAvailable = new SplObjectStorage(); $this->packedBoxSorter = new DefaultPackedBoxSorter(); $this->logger = new NullLogger(); @@ -112,7 +112,7 @@ public function setBoxes(BoxList $boxList): void */ public function setBoxQuantity(Box $box, int $qty): void { - $this->boxesQtyAvailable[$box] = $qty; + $this->boxQuantitiesAvailable[$box] = $qty; } /** @@ -150,7 +150,7 @@ public function pack(): PackedBoxList // If we have multiple boxes, try and optimise/even-out weight distribution if (!$this->beStrictAboutItemOrdering && $packedBoxes->count() > 1 && $packedBoxes->count() <= $this->maxBoxesToBalanceWeight) { - $redistributor = new WeightRedistributor($this->boxes, $this->packedBoxSorter, $this->boxesQtyAvailable); + $redistributor = new WeightRedistributor($this->boxes, $this->packedBoxSorter, $this->boxQuantitiesAvailable); $redistributor->setLogger($this->logger); $packedBoxes = $redistributor->redistributeWeight($packedBoxes); } @@ -203,7 +203,7 @@ public function doVolumePacking(bool $singlePassMode = false, bool $enforceSingl $this->items->removePackedItems($bestBox->getItems()); $packedBoxes->insert($bestBox); - $this->boxesQtyAvailable[$bestBox->getBox()] = $this->boxesQtyAvailable[$bestBox->getBox()] - 1; + $this->boxQuantitiesAvailable[$bestBox->getBox()] = $this->boxQuantitiesAvailable[$bestBox->getBox()] - 1; } return $packedBoxes; @@ -224,7 +224,7 @@ protected function getBoxList(bool $enforceSingleBox = false): iterable $preferredBoxes = []; $otherBoxes = []; foreach ($this->boxes as $box) { - if ($this->boxesQtyAvailable[$box] > 0) { + if ($this->boxQuantitiesAvailable[$box] > 0) { if ($box->getInnerWidth() * $box->getInnerLength() * $box->getInnerDepth() >= $itemVolume) { $preferredBoxes[] = $box; } elseif (!$enforceSingleBox) { diff --git a/src/WeightRedistributor.php b/src/WeightRedistributor.php index 74a9f11..50b82f8 100644 --- a/src/WeightRedistributor.php +++ b/src/WeightRedistributor.php @@ -41,7 +41,7 @@ class WeightRedistributor implements LoggerAwareInterface * * @var SplObjectStorage|int[] */ - private $boxesQtyAvailable; + private $boxQuantitiesAvailable; private PackedBoxSorter $packedBoxSorter; @@ -49,7 +49,7 @@ public function __construct(BoxList $boxList, PackedBoxSorter $packedBoxSorter, { $this->boxes = $boxList; $this->packedBoxSorter = $packedBoxSorter; - $this->boxesQtyAvailable = $boxQuantitiesAvailable; + $this->boxQuantitiesAvailable = $boxQuantitiesAvailable; $this->logger = new NullLogger(); } @@ -128,8 +128,8 @@ private function equaliseWeight(PackedBox &$boxA, PackedBox &$boxB, float $targe if (count($overWeightBoxItems) === 1) { // sometimes a repack can be efficient enough to eliminate a box $boxB = $newLighterBoxes->top(); $boxA = null; - $this->boxesQtyAvailable[$underWeightBox->getBox()] = $this->boxesQtyAvailable[$underWeightBox->getBox()] - 1; - $this->boxesQtyAvailable[$overWeightBox->getBox()] = $this->boxesQtyAvailable[$overWeightBox->getBox()] + 1; + $this->boxQuantitiesAvailable[$underWeightBox->getBox()] = $this->boxQuantitiesAvailable[$underWeightBox->getBox()] - 1; + $this->boxQuantitiesAvailable[$overWeightBox->getBox()] = $this->boxQuantitiesAvailable[$overWeightBox->getBox()] + 1; return true; } @@ -140,10 +140,10 @@ private function equaliseWeight(PackedBox &$boxA, PackedBox &$boxB, float $targe continue; // this should never happen, if we can pack n+1 into the box, we should be able to pack n } - $this->boxesQtyAvailable[$overWeightBox->getBox()] = $this->boxesQtyAvailable[$overWeightBox->getBox()] + 1; - $this->boxesQtyAvailable[$underWeightBox->getBox()] = $this->boxesQtyAvailable[$underWeightBox->getBox()] + 1; - $this->boxesQtyAvailable[$newHeavierBoxes->top()->getBox()] = $this->boxesQtyAvailable[$newHeavierBoxes->top()->getBox()] - 1; - $this->boxesQtyAvailable[$newLighterBoxes->top()->getBox()] = $this->boxesQtyAvailable[$newLighterBoxes->top()->getBox()] - 1; + $this->boxQuantitiesAvailable[$overWeightBox->getBox()] = $this->boxQuantitiesAvailable[$overWeightBox->getBox()] + 1; + $this->boxQuantitiesAvailable[$underWeightBox->getBox()] = $this->boxQuantitiesAvailable[$underWeightBox->getBox()] + 1; + $this->boxQuantitiesAvailable[$newHeavierBoxes->top()->getBox()] = $this->boxQuantitiesAvailable[$newHeavierBoxes->top()->getBox()] - 1; + $this->boxQuantitiesAvailable[$newLighterBoxes->top()->getBox()] = $this->boxQuantitiesAvailable[$newLighterBoxes->top()->getBox()] - 1; $underWeightBox = $boxB = $newLighterBoxes->top(); $overWeightBox = $boxA = $newHeavierBoxes->top(); @@ -162,9 +162,9 @@ private function doVolumeRepack(iterable $items, Box $currentBox): PackedBoxList $packer->setLogger($this->logger); $packer->setBoxes($this->boxes); // use the full set of boxes to allow smaller/larger for full efficiency foreach ($this->boxes as $box) { - $packer->setBoxQuantity($box, $this->boxesQtyAvailable[$box]); + $packer->setBoxQuantity($box, $this->boxQuantitiesAvailable[$box]); } - $packer->setBoxQuantity($currentBox, $this->boxesQtyAvailable[$currentBox] + 1); + $packer->setBoxQuantity($currentBox, $this->boxQuantitiesAvailable[$currentBox] + 1); $packer->setItems($items); return $packer->doVolumePacking(true, true);