From 361ba20d7742d1f74cc9ec95f70519fb998ab169 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Fri, 1 Jan 2021 16:47:32 +0000 Subject: [PATCH] #216 Add getVolume helper method to PackedItemList --- CHANGELOG.md | 9 +++++++-- src/PackedBox.php | 9 +-------- src/PackedItemList.php | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 186a0727..20c42977 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog -## [3.x - Unreleased] - 2020-xx-xx +## [3.x - Unreleased] - 2021-xx-xx + +## [3.7.0] - 2021-01-01 +### Added +- Added `getVolume` helper method to `PackedItemList` ## [3.6.2] - 2020-09-28 ### Added @@ -441,8 +445,9 @@ Initial release - Experimental code to get a feel for how calculations can best be implemented - Only works if all items fit into a single box (so not production ready at all) -[3.x - Unreleased]: https://github.com/dvdoug/BoxPacker/compare/3.6.2...3.x +[3.x - Unreleased]: https://github.com/dvdoug/BoxPacker/compare/3.7.0...3.x +[3.7.0]: https://github.com/dvdoug/BoxPacker/compare/3.6.2...3.7.0 [3.6.2]: https://github.com/dvdoug/BoxPacker/compare/3.6.1...3.6.2 [3.6.1]: https://github.com/dvdoug/BoxPacker/compare/3.6.0...3.6.1 [3.6.0]: https://github.com/dvdoug/BoxPacker/compare/3.5.2...3.6.0 diff --git a/src/PackedBox.php b/src/PackedBox.php index bc6c1303..eda6fac2 100644 --- a/src/PackedBox.php +++ b/src/PackedBox.php @@ -179,14 +179,7 @@ public function getInnerVolume(): int */ public function getUsedVolume(): int { - $volume = 0; - - /** @var PackedItem $item */ - foreach ($this->items as $item) { - $volume += $item->getVolume(); - } - - return $volume; + return $this->items->getVolume(); } /** diff --git a/src/PackedItemList.php b/src/PackedItemList.php index 537fc695..76c8dd8d 100644 --- a/src/PackedItemList.php +++ b/src/PackedItemList.php @@ -77,6 +77,20 @@ public function asItemArray(): array }, $this->list); } + /** + * Get total volume of these items. + */ + public function getVolume(): int + { + $volume = 0; + + foreach ($this->list as $item) { + $volume += $item->getVolume(); + } + + return $volume; + } + private function compare(PackedItem $itemA, PackedItem $itemB): int { $itemAVolume = $itemA->getItem()->getWidth() * $itemA->getItem()->getLength() * $itemA->getItem()->getDepth();