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
  • Loading branch information
dvdoug committed Jun 15, 2018
1 parent 344b280 commit 5100397
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/OrientatedItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function count;
use function min;
use function reset;
use function sort;
use function usort;

/**
Expand Down Expand Up @@ -124,8 +125,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 {

Expand Down Expand Up @@ -238,4 +238,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): bool
{
$itemADimensions = [$itemA->getWidth(), $itemA->getLength(), $itemA->getDepth()];
$itemBDimensions = [$itemB->getWidth(), $itemB->getLength(), $itemB->getDepth()];
sort($itemADimensions);
sort($itemBDimensions);

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

use DVDoug\BoxPacker\Item;
use stdClass;

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

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

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

/**
* TestItem constructor.
*
Expand All @@ -66,6 +79,12 @@ public function __construct(
$this->depth = $depth;
$this->weight = $weight;
$this->keepFlat = $keepFlat;

$this->a = new stdClass();
$this->b = new stdClass();

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

/**
Expand Down

0 comments on commit 5100397

Please sign in to comment.