diff --git a/README.md b/README.md index c919afc..2dbea1f 100755 --- a/README.md +++ b/README.md @@ -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) { @@ -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) { diff --git a/src/ArrayComparator/ArrayComparator.php b/src/ArrayComparator/ArrayComparator.php index 08314f4..d46cbf0 100755 --- a/src/ArrayComparator/ArrayComparator.php +++ b/src/ArrayComparator/ArrayComparator.php @@ -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) @@ -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; @@ -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 @@ -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) diff --git a/tests/ArrayComparator/ArrayComparatorTest.php b/tests/ArrayComparator/ArrayComparatorTest.php index 9cb4264..352a09e 100755 --- a/tests/ArrayComparator/ArrayComparatorTest.php +++ b/tests/ArrayComparator/ArrayComparatorTest.php @@ -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()); } /** @@ -45,6 +50,11 @@ public function testWhenMissingRight() { $comparator = new ArrayComparator(); + $comparator->whenEqual( + function () { + throw new \Exception(); + } + ); $comparator->whenDifferent( function () { throw new \Exception(); @@ -77,6 +87,11 @@ public function testWhenMissingLeft() { $comparator = new ArrayComparator(); + $comparator->whenEqual( + function () { + throw new \Exception(); + } + ); $comparator->whenDifferent( function () { throw new \Exception(); @@ -109,6 +124,11 @@ public function testWhenDifferences() { $comparator = new ArrayComparator(); + $comparator->whenEqual( + function () { + throw new \Exception(); + } + ); $comparator->whenMissingRight( function () { throw new \Exception(); @@ -142,6 +162,11 @@ public function testWhenDifferencesWithIndexedArray() { $comparator = new ArrayComparator(); + $comparator->whenEqual( + function () { + throw new \Exception(); + } + ); $comparator->whenMissingRight( function () { throw new \Exception(); @@ -196,6 +221,11 @@ function ($item1, $item2) { } ); + $comparator->whenEqual( + function () { + throw new \Exception(); + } + ); $comparator->whenMissingRight( function () { throw new \Exception(); @@ -237,6 +267,11 @@ public function testWhenDifferencesWithCustomComparatorClass() $comparator = new CustomComparator(); + $comparator->whenEqual( + function () { + throw new \Exception(); + } + ); $comparator->whenMissingRight( function () { throw new \Exception(); @@ -291,6 +326,15 @@ function ($item1, $item2) { } ); + $testCase = $this; + + $callCountEqual = 0; + $comparator->whenEqual( + function () use (&$callCountEqual, $testCase) { + $callCountEqual++; + } + ); + $comparator->whenMissingRight( function () { throw new \Exception(); @@ -309,6 +353,8 @@ function () { ); $comparator->compare(array($object1), array($object2)); + + $this->assertEquals(1, $callCountEqual); } /** @@ -336,6 +382,11 @@ function ($item) use (&$callCountLeft, $testCase) { } ); + $comparator->whenEqual( + function () { + throw new \Exception(); + } + ); $comparator->whenDifferent( function () { throw new \Exception(); @@ -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); + } + } /**