diff --git a/README.md b/README.md index 3b10348..719edac 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,14 @@ $old = [ ], ]; -var_dump(ArrayDiffMultidimensional::compare($new, $old)); +// Compare the arrays by calling the 'compare' class method +$result = ArrayDiffMultidimensional::compare($new, $old) +// Or by calling the global helper function +$result = array_diff_multidimensional($new, $old) + + +var_dump($result); ``` The result of comparing `$new` with `$old` will return a new array with the changes: @@ -56,9 +62,12 @@ The result of comparing `$new` with `$old` will return a new array with the chan **Comparisons are strict by default**, but you can specify that you want to make a loose comparison passing a boolean as the third parameter for `compare` method or calling the `looseComparison` ```php -// This will deactivate the strict comparison mode +// Passing 'false' as a third parameter will deactivate the strict comparison mode ArrayDiffMultidimensional::compare($new, $old, false); +array_diff_multidimensional($new, $old, false); + + // This method call is equivalent ArrayDiffMultidimensional::looseComparison($new, $old); ``` @@ -68,6 +77,9 @@ Also, a `strictComparison` method is available for more clarity // Comparisons are strict by default ArrayDiffMultidimensional::compare($new, $old); +array_diff_multidimensional($new, $old); + + // This method call is equivalent ArrayDiffMultidimensional::strictComparison($new, $old); ``` diff --git a/composer.json b/composer.json index de03cb3..5dd8a1e 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,10 @@ "autoload": { "psr-4": { "Rogervila\\": "src/" - } + }, + "files": [ + "src/helpers.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..14a69ce --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,21 @@ +assertTrue(is_array(array_diff_multidimensional([], []))); + } + + /** @test */ + public function it_calls_compare_method() + { + $new = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => 'Hello', + ], + ]; + + $old = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e', + 'f' => 'Goodbye', + ], + ]; + + $this->assertEquals( + [ + 'c' => [ + 'f' => 'Hello' + ], + ], + array_diff_multidimensional($new, $old) + ); + } + + /** @test */ + public function loose_comparisons() + { + $new = [ + 'a' => 'b', + 'c' => 288, + ]; + + $old = [ + 'a' => 'b', + 'c' => '288', + ]; + + $this->assertEquals(0, count(array_diff_multidimensional($new, $old, false))); + $this->assertFalse(isset(array_diff_multidimensional($new, $old, false)['c'])); + } + + /** @test */ + public function strict_comparisons() + { + $new = [ + 'a' => 'b', + 'c' => 288, + ]; + + $old = [ + 'a' => 'b', + 'c' => '288', + ]; + + $this->assertEquals(1, count(array_diff_multidimensional($new, $old))); + $this->assertTrue(isset(array_diff_multidimensional($new, $old)['c'])); + $this->assertEquals(288, array_diff_multidimensional($new, $old)['c']); + + $this->assertEquals(1, count(array_diff_multidimensional($new, $old, true))); + $this->assertTrue(isset(array_diff_multidimensional($new, $old, true)['c'])); + $this->assertEquals(288, array_diff_multidimensional($new, $old, true)['c']); + } +}