Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ By default, array keys are compared. This behavior can be customized.
```php
$comparator = new ArrayComparator();

$comparator->whenDifferent(function ($item1, $item2) {
$comparator->whenEqual(function ($item1, $item2) {
// Do your stuff !
})
->whenDifferent(function ($item1, $item2) {
// Do your stuff !
})
->whenMissingRight(function ($item1) {
Expand Down Expand Up @@ -74,7 +77,14 @@ $comparator->whenDifferent(array($this, 'whenDifferent'));

## Documentation

* `whenDifferent` - Called when 2 items are found in both arrays, but are differents
* `whenEqual` - Called when 2 items are found in both arrays, and are equal

```php
$comparator->whenEqual(function ($item1, $item2) {
});
```

* `whenDifferent` - Called when 2 items are found in both arrays, but are different

```php
$comparator->whenDifferent(function ($item1, $item2) {
Expand Down
24 changes: 23 additions & 1 deletion src/ArrayComparator/ArrayComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class ArrayComparator
*/
private $itemComparator;

/**
* Closure executed when items are equal
* @var callable function($item1, $item2)
*/
private $whenEqual;

/**
* Closure executed when items are different
* @var callable function($item1, $item2)
Expand Down Expand Up @@ -57,6 +63,7 @@ public function __construct()
public function compare(array $array1, array $array2)
{
$compareItems = $this->itemComparator;
$whenEqual = $this->whenEqual;
$whenDifferent = $this->whenDifferent;
$whenMissingLeft = $this->whenMissingLeft;
$whenMissingRight = $this->whenMissingRight;
Expand All @@ -67,9 +74,12 @@ public function compare(array $array1, array $array2)
if ($item2 !== null) {
// Compare 2 items
$itemsAreEqual = call_user_func($compareItems, $item1, $item2);
if (!$itemsAreEqual) {
if (!$itemsAreEqual && $whenDifferent) {
// Items are different
call_user_func($whenDifferent, $item1, $item2);
} elseif ($itemsAreEqual && $whenEqual) {
// Items are equal
call_user_func($whenEqual, $item1, $item2);
}
} elseif ($whenMissingRight) {
// Item from left array is missing from right array
Expand Down Expand Up @@ -111,6 +121,18 @@ public function setItemComparator($callback)
return $this;
}

/**
* Closure executed when items are equal
* @param callable $callback function($item1, $item2)
* @return $this
*/
public function whenEqual($callback)
{
$this->whenEqual = $callback;

return $this;
}

/**
* Closure executed when items are different
* @param callable $callback function($item1, $item2)
Expand Down
138 changes: 126 additions & 12 deletions tests/ArrayComparator/ArrayComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ public function testCompareEmptyArrays()
{
$comparator = new ArrayComparator();

$comparator->whenDifferent(
function () {
throw new \Exception();
}
)
->whenMissingLeft(
$comparator->whenEqual(
function () {
throw new \Exception();
}
)
->whenMissingRight(
function () {
throw new \Exception();
}
)
->compare(array(), array());
->whenDifferent(
function () {
throw new \Exception();
}
)
->whenMissingLeft(
function () {
throw new \Exception();
}
)
->whenMissingRight(
function () {
throw new \Exception();
}
)
->compare(array(), array());
}

/**
Expand All @@ -45,6 +50,11 @@ public function testWhenMissingRight()
{
$comparator = new ArrayComparator();

$comparator->whenEqual(
function () {
throw new \Exception();
}
);
$comparator->whenDifferent(
function () {
throw new \Exception();
Expand Down Expand Up @@ -77,6 +87,11 @@ public function testWhenMissingLeft()
{
$comparator = new ArrayComparator();

$comparator->whenEqual(
function () {
throw new \Exception();
}
);
$comparator->whenDifferent(
function () {
throw new \Exception();
Expand Down Expand Up @@ -109,6 +124,11 @@ public function testWhenDifferences()
{
$comparator = new ArrayComparator();

$comparator->whenEqual(
function () {
throw new \Exception();
}
);
$comparator->whenMissingRight(
function () {
throw new \Exception();
Expand Down Expand Up @@ -142,6 +162,11 @@ public function testWhenDifferencesWithIndexedArray()
{
$comparator = new ArrayComparator();

$comparator->whenEqual(
function () {
throw new \Exception();
}
);
$comparator->whenMissingRight(
function () {
throw new \Exception();
Expand Down Expand Up @@ -196,6 +221,11 @@ function ($item1, $item2) {
}
);

$comparator->whenEqual(
function () {
throw new \Exception();
}
);
$comparator->whenMissingRight(
function () {
throw new \Exception();
Expand Down Expand Up @@ -237,6 +267,11 @@ public function testWhenDifferencesWithCustomComparatorClass()

$comparator = new CustomComparator();

$comparator->whenEqual(
function () {
throw new \Exception();
}
);
$comparator->whenMissingRight(
function () {
throw new \Exception();
Expand Down Expand Up @@ -291,6 +326,15 @@ function ($item1, $item2) {
}
);

$testCase = $this;

$callCountEqual = 0;
$comparator->whenEqual(
function () use (&$callCountEqual, $testCase) {
$callCountEqual++;
}
);

$comparator->whenMissingRight(
function () {
throw new \Exception();
Expand All @@ -309,6 +353,8 @@ function () {
);

$comparator->compare(array($object1), array($object2));

$this->assertEquals(1, $callCountEqual);
}

/**
Expand Down Expand Up @@ -336,6 +382,11 @@ function ($item) use (&$callCountLeft, $testCase) {
}
);

$comparator->whenEqual(
function () {
throw new \Exception();
}
);
$comparator->whenDifferent(
function () {
throw new \Exception();
Expand All @@ -348,6 +399,69 @@ function () {
$this->assertEquals(1, $callCountLeft);
}

/**
* Test missing items from both arrays, with an indexed array
*/
public function testComplexArray()
{
$comparator = new ArrayComparator();

$testCase = $this;

$callCountEqual = 0;
$comparator->whenEqual(
function ($item) use (&$callCountEqual, $testCase) {
$callCountEqual++;
}
);

$callCountDifferent = 0;
$comparator->whenDifferent(
function ($item) use (&$callCountDifferent, $testCase) {
$callCountDifferent++;
}
);

$callCountRight = 0;
$comparator->whenMissingRight(
function ($item) use (&$callCountRight, $testCase) {
$callCountRight++;
}
);

$callCountLeft = 0;
$comparator->whenMissingLeft(
function ($item) use (&$callCountLeft, $testCase) {
$callCountLeft++;
}
);

$comparator->compare(
array('foo' => '1', 'fuu' => '2', 'fii' => '3', 'bar' => '4'),
array('bim' => 'baz', 'foo' => '1', 'fuu' => '21', 'bar' => '51')
);

$this->assertEquals(1, $callCountEqual);
$this->assertEquals(2, $callCountDifferent);
$this->assertEquals(1, $callCountRight);
$this->assertEquals(1, $callCountLeft);
}

/**
* Test missing items from both arrays, with an indexed array
*/
public function testNoCallableWorking()
{
$comparator = new ArrayComparator();

$comparator->compare(
array('foo' => '1', 'fuu' => '2', 'fii' => '3', 'bar' => '4'),
array('bim' => 'baz', 'foo' => '1', 'fuu' => '21', 'bar' => '51')
);

$this->assertTrue(true);
}

}

/**
Expand Down