Skip to content
Permalink
Browse files

allow specifying an explicit root

  • Loading branch information...
AD7six committed Jan 18, 2012
1 parent 7414d0f commit f1f1ce0db434ff38991839c9e01509422f127c17
Showing with 131 additions and 14 deletions.
  1. +110 −0 lib/Cake/Test/Case/Utility/SetTest.php
  2. +21 −14 lib/Cake/Utility/Set.php
@@ -3286,6 +3286,116 @@ public function testNestModel() {
$this->assertEquals($expected, $result);
}
/**
* test Set nest with a normal model result set, and a nominated root id
*
* @return void
*/
public function testNestModelExplicitRoot() {
$input = array(
array(
'ModelName' => array(
'id' => 1,
'parent_id' => null
),
),
array(
'ModelName' => array(
'id' => 2,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 3,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 4,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 5,
'parent_id' => 1
),
),
array(
'ModelName' => array(
'id' => 6,
'parent_id' => null
),
),
array(
'ModelName' => array(
'id' => 7,
'parent_id' => 6
),
),
array(
'ModelName' => array(
'id' => 8,
'parent_id' => 6
),
),
array(
'ModelName' => array(
'id' => 9,
'parent_id' => 6
),
),
array(
'ModelName' => array(
'id' => 10,
'parent_id' => 6
)
)
);
$expected = array(
array(
'ModelName' => array(
'id' => 6,
'parent_id' => null
),
'children' => array(
array(
'ModelName' => array(
'id' => 7,
'parent_id' => 6
),
'children' => array()
),
array(
'ModelName' => array(
'id' => 8,
'parent_id' => 6
),
'children' => array()
),
array(
'ModelName' => array(
'id' => 9,
'parent_id' => 6
),
'children' => array()
),
array(
'ModelName' => array(
'id' => 10,
'parent_id' => 6
),
'children' => array()
)
)
)
);
$result = Set::nest($input, array('root' => 6));
$this->assertEquals($expected, $result);
}
/**
* test Set nest with a 1d array - this method should be able to handle any type of array input
*
@@ -1120,9 +1120,10 @@ public static function apply($path, $data, $callback, $options = array()) {
*
* @param mixed $data
* @param array $options Options are:
* children - the key name to use in the resultset for children
* idPath - the path to a key that identifies each entry
* children - the key name to use in the resultset for children
* idPath - the path to a key that identifies each entry
* parentPath - the path to a key that identifies the parent of each entry
* root - the id of the desired top-most result
* @return array of results, nested
* @link
*/
@@ -1135,7 +1136,8 @@ public static function nest($data, $options = array()) {
$options += array(
'idPath' => "/$alias/id",
'parentPath' => "/$alias/parent_id",
'children' => 'children'
'children' => 'children',
'root' => null
);
$return = $idMap = array();
@@ -1161,16 +1163,21 @@ public static function nest($data, $options = array()) {
}
}
$root = Set::get($return[0], $parentKeys);
if ($options['root']) {
$root = $options['root'];
} else {
$root = Set::get($return[0], $parentKeys);
}
foreach ($return as $i => $result) {
$id = Set::get($result, $idKeys);
$parentId = Set::get($result, $parentKeys);
if ($parentId != $root) {
if ($id !== $root && $parentId != $root) {
unset($return[$i]);
}
}
return $return;
return array_values($return);
}
/**
@@ -1182,17 +1189,17 @@ public static function nest($data, $options = array()) {
*/
public static function get($input, $path = null) {
if (is_string($path)) {
if (strpos($path, '/') !== false) {
$keys = explode('/', trim($path, '/'));
} else {
$keys = explode('.', trim($path, '.'));
}
if (strpos($path, '/') !== false) {
$keys = explode('/', trim($path, '/'));
} else {
$keys = explode('.', trim($path, '.'));
}
} else {
$keys = $path;
}
if (!$keys) {
return $input;
}
if (!$keys) {
return $input;
}
$return = $input;
foreach($keys as $key) {

0 comments on commit f1f1ce0

Please sign in to comment.
You can’t perform that action at this time.