Merge branch 'hash-maxdim' into 2.7

Merges pull requests #7041 and #7040 into 2.7
This commit is contained in:
mark_story 2015-07-20 22:26:55 -04:00
commit 27e40f084d
2 changed files with 18 additions and 34 deletions

View file

@ -300,38 +300,25 @@ class HashTest extends CakeTestCase {
$result = Hash::maxDimensions($data);
$this->assertEquals($result, 2);
$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
$data = array(
'1' => array('1.1' => '1.1.1'),
'2',
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Hash::maxDimensions($data);
$this->assertEquals($result, 3);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Hash::maxDimensions($data);
$this->assertEquals($result, 4);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Hash::maxDimensions($data);
$this->assertEquals($result, 5);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Hash::maxDimensions($data);
$this->assertEquals($result, 5);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
'1' => array(
'1.1' => '1.1.1',
'1.2' => array(
'1.2.1' => array(
'1.2.1.1',
array('1.2.2.1')
)
)
),
'2' => array('2.1' => '2.1.1',)
);
$result = Hash::maxDimensions($data);
$this->assertEquals($result, 5);

View file

@ -756,18 +756,15 @@ class Hash {
* number of dimensions in a mixed array.
*
* @param array $data Array to count dimensions on
* @param int $count counts current depth of this iteration
* @return int The maximum number of dimensions in $data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions
*/
public static function maxDimensions(array $data) {
public static function maxDimensions($data) {
$depth = array();
if (is_array($data) && reset($data) !== false) {
foreach ($data as $value) {
if (is_array($value)) {
$depth[] = self::dimensions($value) + 1;
} else {
$depth[] = 1;
}
$depth[] = static::maxDimensions($value) + 1;
}
}
return empty($depth) ? 0 : max($depth);