mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
allow specifying an explicit root
This commit is contained in:
parent
7414d0f77b
commit
f1f1ce0db4
2 changed files with 131 additions and 14 deletions
|
@ -3286,6 +3286,116 @@ class SetTest extends CakeTestCase {
|
|||
$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 @@ class Set {
|
|||
*
|
||||
* @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 @@ class Set {
|
|||
$options += array(
|
||||
'idPath' => "/$alias/id",
|
||||
'parentPath' => "/$alias/parent_id",
|
||||
'children' => 'children'
|
||||
'children' => 'children',
|
||||
'root' => null
|
||||
);
|
||||
|
||||
$return = $idMap = array();
|
||||
|
@ -1161,16 +1163,21 @@ class Set {
|
|||
}
|
||||
}
|
||||
|
||||
$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 @@ class Set {
|
|||
*/
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue