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); $result = Hash::maxDimensions($data);
$this->assertEquals($result, 2); $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); $result = Hash::maxDimensions($data);
$this->assertEquals($result, 3); $this->assertEquals($result, 3);
$data = array( $data = array(
'1' => array('1.1' => '1.1.1'), '1' => array(
array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '1.1' => '1.1.1',
'3' => array('3.1' => array('3.1.1' => '3.1.1.1')) '1.2' => array(
); '1.2.1' => array(
$result = Hash::maxDimensions($data); '1.2.1.1',
$this->assertEquals($result, 4); array('1.2.2.1')
)
$data = array( )
'1' => array('1.1' => '1.1.1'), ),
array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '2' => array('2.1' => '2.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'))
); );
$result = Hash::maxDimensions($data); $result = Hash::maxDimensions($data);
$this->assertEquals($result, 5); $this->assertEquals($result, 5);

View file

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