diff --git a/cake/libs/set.php b/cake/libs/set.php index b2d949519..464b69c3c 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -598,22 +598,35 @@ class Set extends Object { return true; } /** - * Counts the dimensions of an array. + * Counts the dimensions of an array. If $all is set to false (which is the default) it will + * only consider the dimension of the first element in the array. * * @param array $array Array to count dimensions on + * @param boolean $all Set to true to count the dimension considering all elements in array + * @param integer $count Start the dimension count at this number * @return integer The number of dimensions in $array * @access public */ - function countDim($array = null) { + function countDim($array = null, $all = false, $count = 0) { if ($array === null) { $array = $this->get(); } elseif (is_object($array) && is_a($array, 'set')) { $array = $array->get(); } - if (is_array(reset($array))) { - $return = Set::countDim(reset($array)) + 1; + if ($all) { + $depth = array($count); + if (is_array($array) && reset($array) !== false) { + foreach ($array as $value) { + $depth[] = Set::countDim($value, true, $count + 1); + } + } + $return = max($depth); } else { - $return = 1; + if (is_array(reset($array))) { + $return = Set::countDim(reset($array)) + 1; + } else { + $return = 1; + } } return $return; } diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index d788b4304..15eb16ab6 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -569,6 +569,48 @@ class SetTest extends UnitTestCase { $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}'); $this->assertEqual($result, $expected); } + + function testCountDim() { + $data = array('one', '2', 'three'); + $result = Set::countDim($data); + $this->assertEqual($result, 1); + + $data = array('1' => '1.1', '2', '3'); + $result = Set::countDim($data); + $this->assertEqual($result, 1); + + $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1')); + $result = Set::countDim($data); + $this->assertEqual($result, 2); + + $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); + $result = Set::countDim($data); + $this->assertEqual($result, 1); + + $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); + $result = Set::countDim($data, true); + $this->assertEqual($result, 2); + + $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); + $result = Set::countDim($data); + $this->assertEqual($result, 2); + + $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); + $result = Set::countDim($data, true); + $this->assertEqual($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 = Set::countDim($data, true); + $this->assertEqual($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 = Set::countDim($data, true); + $this->assertEqual($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 = Set::countDim($data, true); + $this->assertEqual($result, 5); + } } ?> \ No newline at end of file