Custom constraints ================== For more advanced use cases where greater control over the contents of each box is required (e.g. legal limits on the number of hazardous items per box, or perhaps fragile items requiring an extra-strong outer box) you may implement the ``BoxPacker\ConstrainedPlacementItem`` interface which contains an additional callback method allowing you to decide whether to allow an item may be packed into a box or not. As with all other library methods, the objects passed into this callback are your own - you have access to their full range of properties and methods to use when evaluating a constraint, not only those defined by the standard ``BoxPacker\Item`` interface. Example - only allow 2 batteries per box ---------------------------------------- .. code-block:: php items as $packedItem) { if ($packedItem->item instanceof LithiumBattery) { $batteriesPacked++; } } if ($batteriesPacked < 2) { return true; // allowed to pack } else { return false; // 2 batteries already packed, no more allowed in this box } } } Example - don't allow batteries to be stacked --------------------------------------------- .. code-block:: php items, false), function (PackedItem $item) { return $item->item->getDescription() === 'Battery'; } ); /** @var PackedItem $alreadyPacked */ foreach ($alreadyPackedType as $alreadyPacked) { if ( $alreadyPacked->z + $alreadyPacked->depth === $proposedZ && $proposedX >= $alreadyPacked->x && $proposedX <= ($alreadyPacked->x + $alreadyPacked->width) && $proposedY >= $alreadyPacked->y && $proposedY <= ($alreadyPacked->y + $alreadyPacked->length)) { return false; } } return true; } }