/** * Remove any ship groups that no longer contain items. * * @param IShipGroupIterable * @return IShipGroupIterable */ protected function removeEmptyShipGroups(IShipGroupIterable $shipGroups, IOrderDestinationIterable $destinations) { // Can't loop over the iterable and modify it at the same time. // Collect items that need to be removed from the iterable but don't // modify the iterable. $emptyShipGroups = []; foreach ($shipGroups as $shipGroup) { if ($shipGroup->getItemReferences()->count() === 0) { $emptyShipGroups[] = $shipGroup; } } // Go through the items that need to be removed and remove each from // the iterable. foreach ($emptyShipGroups as $removeShipGroup) { // Destinations are stored separate from the ship group. Any destinations // for empty ship groups are no longer necessary so remove them with // the rest of the ship group. $destinations->offsetUnset($removeShipGroup->getDestination()); $shipGroups->offsetUnset($removeShipGroup); } return $shipGroups; }