Skip to content

Commit

Permalink
Compare dimensions explicitly when looking for identically sized obje…
Browse files Browse the repository at this point in the history
…cts rather than relying on simple object equality. Fixes #142

Signed-off-by: Doug Wright <[email protected]>
  • Loading branch information
dvdoug committed Jun 15, 2018
1 parent 00e419d commit 1501f7a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/OrientatedItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public function getPossibleOrientations(
$orientations = [];

//Special case items that are the same as what we just packed - keep orientation
/* @noinspection PhpNonStrictObjectEqualityInspection */
if ($prevItem && $prevItem->getItem() == $item) {
if ($prevItem && $this->isSameDimensions($prevItem->getItem(), $item)) {
$orientations[] = new OrientatedItem($item, $prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth());
} else {
//simple 2D rotation
Expand Down Expand Up @@ -221,4 +220,20 @@ function (OrientatedItem $orientation) {
}
);
}

/**
* Compare two items to see if they have same dimensions
* @param Item $itemA
* @param Item $itemB
* @return bool
*/
protected function isSameDimensions(Item $itemA, Item $itemB)
{
$itemADimensions = [$itemA->getWidth(), $itemA->getLength(), $itemA->getDepth()];
$itemBDimensions = [$itemB->getWidth(), $itemB->getLength(), $itemB->getDepth()];
sort($itemADimensions);
sort($itemBDimensions);

return $itemADimensions === $itemBDimensions;
}
}
18 changes: 18 additions & 0 deletions tests/Test/TestItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace DVDoug\BoxPacker\Test;

use DVDoug\BoxPacker\Item;
use stdClass;

class TestItem implements Item
{
Expand Down Expand Up @@ -41,6 +42,18 @@ class TestItem implements Item
*/
private $volume;


/* Test objects that recurse
* @var stdClass
*/
private $a;

/**
* Test objects that recurse
* @var stdClass
*/
private $b;

/**
* TestItem constructor.
*
Expand All @@ -64,6 +77,11 @@ public function __construct(
$this->weight = $weight;

$this->volume = $this->width * $this->length * $this->depth;
$this->a = new stdClass();
$this->b = new stdClass();

$this->a->b = $this->b;
$this->b->a = $this->a;
}

/**
Expand Down

0 comments on commit 1501f7a

Please sign in to comment.