Skip to content

Commit

Permalink
Merge af117a8 into c10cce4
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Jan 1, 2018
2 parents c10cce4 + af117a8 commit 8693d2d
Show file tree
Hide file tree
Showing 24 changed files with 437 additions and 392 deletions.
40 changes: 25 additions & 15 deletions src/Box.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,85 @@
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* Box packing (3D bin packing, knapsack problem).
*
* @author Doug Wright
*/

namespace DVDoug\BoxPacker;

/**
* A "box" (or envelope?) to pack items into
* A "box" (or envelope?) to pack items into.
*
* @author Doug Wright
* @package BoxPacker
*/
interface Box
{

/**
* Reference for box type (e.g. SKU or description)
* Reference for box type (e.g. SKU or description).
*
* @return string
*/
public function getReference();

/**
* Outer width in mm
* Outer width in mm.
*
* @return int
*/
public function getOuterWidth();

/**
* Outer length in mm
* Outer length in mm.
*
* @return int
*/
public function getOuterLength();

/**
* Outer depth in mm
* Outer depth in mm.
*
* @return int
*/
public function getOuterDepth();

/**
* Empty weight in g
* Empty weight in g.
*
* @return int
*/
public function getEmptyWeight();

/**
* Inner width in mm
* Inner width in mm.
*
* @return int
*/
public function getInnerWidth();

/**
* Inner length in mm
* Inner length in mm.
*
* @return int
*/
public function getInnerLength();

/**
* Inner depth in mm
* Inner depth in mm.
*
* @return int
*/
public function getInnerDepth();

/**
* Total inner volume of packing in mm^3
* Total inner volume of packing in mm^3.
*
* @return int
*/
public function getInnerVolume();

/**
* Max weight the packaging can hold in g
* Max weight the packaging can hold in g.
*
* @return int
*/
public function getMaxWeight();
Expand Down
10 changes: 6 additions & 4 deletions src/BoxList.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* Box packing (3D bin packing, knapsack problem).
*
* @author Doug Wright
*/

namespace DVDoug\BoxPacker;

/**
* List of boxes available to put items into, ordered by volume
* List of boxes available to put items into, ordered by volume.
*
* @author Doug Wright
* @package BoxPacker
*/
class BoxList extends \SplMinHeap
{
/**
* Compare elements in order to place them correctly in the heap while sifting up.
*
* @see \SplMinHeap::compare()
*
* @param Box $boxA
Expand Down
14 changes: 6 additions & 8 deletions src/ConstrainedItem.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* Box packing (3D bin packing, knapsack problem).
*
* @author Doug Wright
*/

namespace DVDoug\BoxPacker;

/**
* An item to be packed where additional constraints need to be considered. Only implement this interface if you actually
* need this additional functionality as it will slow down the packing algorithm
* need this additional functionality as it will slow down the packing algorithm.
*
* @author Doug Wright
* @package BoxPacker
*/
interface ConstrainedItem extends Item
{

/**
* Hook for user implementation of item-specific constraints, e.g. max <x> batteries per box
* Hook for user implementation of item-specific constraints, e.g. max <x> batteries per box.
*
* @param ItemList $alreadyPackedItems
* @param Box $box
*
* @return bool
*/
public function canBePackedInBox(ItemList $alreadyPackedItems, Box $box);

}

31 changes: 18 additions & 13 deletions src/Item.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* Box packing (3D bin packing, knapsack problem).
*
* @author Doug Wright
*/

namespace DVDoug\BoxPacker;

/**
* An item to be packed
* An item to be packed.
*
* @author Doug Wright
* @package BoxPacker
*/
interface Item
{

/**
* Item SKU etc
* Item SKU etc.
*
* @return string
*/
public function getDescription();

/**
* Item width in mm
* Item width in mm.
*
* @return int
*/
public function getWidth();

/**
* Item length in mm
* Item length in mm.
*
* @return int
*/
public function getLength();

/**
* Item depth in mm
* Item depth in mm.
*
* @return int
*/
public function getDepth();

/**
* Item weight in g
* Item weight in g.
*
* @return int
*/
public function getWeight();

/**
* Item volume in mm^3
* Item volume in mm^3.
*
* @return int
*/
public function getVolume();

/**
* Does this item need to be kept flat / packed "this way up"?
*
* @return bool
*/
public function getKeepFlat();

}

16 changes: 9 additions & 7 deletions src/ItemList.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* Box packing (3D bin packing, knapsack problem).
*
* @author Doug Wright
*/

namespace DVDoug\BoxPacker;

/**
* List of items to be packed, ordered by volume
* List of items to be packed, ordered by volume.
*
* @author Doug Wright
* @package BoxPacker
*/
class ItemList extends \SplMaxHeap
{

/**
* Compare elements in order to place them correctly in the heap while sifting up.
*
Expand All @@ -32,15 +32,16 @@ public function compare($itemA, $itemB)
return -1;
} elseif ($itemA->getWeight() !== $itemB->getWeight()) {
return $itemA->getWeight() - $itemB->getWeight();
} else if ($itemA->getDescription() < $itemB->getDescription()) {
} elseif ($itemA->getDescription() < $itemB->getDescription()) {
return 1;
} else {
return -1;
}
}

/**
* Get copy of this list as a standard PHP array
* Get copy of this list as a standard PHP array.
*
* @return array
*/
public function asArray()
Expand All @@ -49,6 +50,7 @@ public function asArray()
foreach (clone $this as $item) {
$return[] = $item;
}

return $return;
}
}
17 changes: 8 additions & 9 deletions src/ItemTooLargeException.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?php

/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* Box packing (3D bin packing, knapsack problem).
*
* @author Doug Wright
*/

namespace DVDoug\BoxPacker;

/**
* Class ItemTooLargeException
* Exception used when an item is too large to pack
*
* @package DVDoug\BoxPacker
* Exception used when an item is too large to pack.
*/
class ItemTooLargeException extends \RuntimeException
{

/** @var Item */
public $item;

Expand All @@ -25,16 +23,17 @@ class ItemTooLargeException extends \RuntimeException
* @param string $message
* @param Item $item
*/
public function __construct($message, Item $item) {
public function __construct($message, Item $item)
{
$this->item = $item;
parent::__construct($message);
}

/**
* @return Item
*/
public function getItem() {
public function getItem()
{
return $this->item;
}

}

0 comments on commit 8693d2d

Please sign in to comment.