Commit afdf1887 authored by webchick's avatar webchick

Issue #1850798 by damiankloip, pfrenssen, chx: Need array_diff_assoc_recursive() for php 5.4.

parent 093088a4
<?php
/**
* @file
* Contains Drupal\Component\Utility\DiffArray.
*/
namespace Drupal\Component\Utility;
/**
* Provides helpers to perform diffs on multi dimensional arrays.
*/
class DiffArray {
/**
* Computes the difference between arrays.
*
* This is just a wrapper around array_diff_assoc(). The only reason Drupal
* provides this function is in since PHP 5.4 casting of arrays to strings
* throws a notice. If you want to use array_diff_assoc() as it was up to PHP
* 5.3, then use this function but consider the consequences -- it should be
* rare to use this function.
*
* @param array $array1
* The array to compare from.
* @param array $array2
* The array to compare to.
*
* @return array
* Returns an array containing all the values from array1 that are not present
* in array2.
*/
public static function diffAssoc(array $array1, array $array2) {
return @array_diff_assoc($array1, $array2);
}
/**
* Recursively computes the difference of arrays with additional index check.
*
* This is a version of array_diff_assoc() that supports multidimensional
* arrays.
*
* @param array $array1
* The array to compare from.
* @param array $array2
* The array to compare to.
*
* @return array
* Returns an array containing all the values from array1 that are not present
* in array2.
*/
public static function diffAssocRecursive(array $array1, array $array2) {
$difference = array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (!array_key_exists($key, $array2) && !is_array($array2[$key])) {
$difference[$key] = $value;
}
else {
$new_diff = static::diffAssocRecursive($value, $array2[$key]);
if (!empty($new_diff)) {
$difference[$key] = $new_diff;
}
}
}
elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
}
<?php
/**
* @file
* Contains \Drupal\system\Tests\Common\DiffArrayUnitTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\Component\Utility\DiffArray;
use Drupal\simpletest\UnitTestBase;
/**
* Tests the DiffArray helper class.
*/
class DiffArrayUnitTest extends UnitTestBase {
/**
* Array to use for testing.
*
* @var array
*/
protected $array1;
/**
* Array to use for testing.
*
* @var array
*/
protected $array2;
public static function getInfo() {
return array(
'name' => 'DiffArray functionality',
'description' => 'Tests the DiffArray helper class.',
'group' => 'System',
);
}
function setUp() {
parent::setUp();
$this->array1 = array(
'same' => 'yes',
'different' => 'no',
'array_empty_diff' => array(),
'null' => NULL,
'int_diff' => 1,
'array_diff' => array('same' => 'same', 'array' => array('same' => 'same')),
'new' => 'new',
);
$this->array2 = array(
'same' => 'yes',
'different' => 'yes',
'array_empty_diff' => array(),
'null' => NULL,
'int_diff' => '1',
'array_diff' => array('same' => 'different', 'array' => array('same' => 'same')),
);
}
/**
* Tests DiffArray::diffAssoc().
*/
public function testDiffAssoc() {
$expected = array(
'different' => 'no',
'new' => 'new',
);
$this->assertIdentical(DiffArray::diffAssoc($this->array1, $this->array2), $expected);
}
/**
* Tests DiffArray::diffAssocRecursive().
*/
public function testDiffAssocRecursive() {
$expected = array(
'different' => 'no',
'int_diff' => 1,
// The 'array' key should not be returned, as it's the same.
'array_diff' => array('same' => 'same'),
'new' => 'new',
);
$this->assertIdentical(DiffArray::diffAssocRecursive($this->array1, $this->array2), $expected);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment