Skip to content

Commit

Permalink
#216 Add getVolume helper method to PackedItemList
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Jan 1, 2021
1 parent a10fed7 commit 361ba20
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions src/PackedBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/PackedItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 361ba20

Please sign in to comment.