Skip to content

Commit

Permalink
array_diff_multidimensional helper function (#21)
Browse files Browse the repository at this point in the history
* Create helpers.php

* Update composer.json

* Create HelperFunctionTest.php

* Update HelperFunctionTest.php

* Update README.md

* Update README.md

Co-authored-by: William Desportes <[email protected]>

Co-authored-by: William Desportes <[email protected]>
  • Loading branch information
rogervila and williamdes authored Oct 28, 2021
1 parent 51d68c0 commit d5afb18
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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);
```
Expand All @@ -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);
```
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"autoload": {
"psr-4": {
"Rogervila\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
21 changes: 21 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Rogervila\ArrayDiffMultidimensional;

if (!function_exists('array_diff_multidimensional')) {

/**
* Returns an array with the differences between $array1 and $array2
* $strict variable defines if comparison must be strict or not
*
* @param array $array1
* @param array $array2
* @param bool $strict
*
* @return array
*/
function array_diff_multidimensional($array1, $array2, $strict = true)
{
return ArrayDiffMultidimensional::compare($array1, $array2, $strict);
}
}
82 changes: 82 additions & 0 deletions tests/HelperFunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Rogervila\Test;

use PHPUnit\Framework\TestCase;

class HelperFunctionTest extends TestCase
{
/** @test */
public function it_returns_an_array()
{
$this->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']);
}
}

0 comments on commit d5afb18

Please sign in to comment.