Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
reduce code repetition
  • Loading branch information
AD7six committed Jan 10, 2012
1 parent 5608d63 commit d534013
Showing 1 changed file with 28 additions and 37 deletions.
65 changes: 28 additions & 37 deletions lib/Cake/Utility/Set.php
Expand Up @@ -1146,21 +1146,8 @@ public static function nest($data, $options = array()) {
foreach ($data as $result) {
$result[$options['children']] = array();

$id = $result;
foreach($idKeys as $key) {
$id = $id[$key];
}

$parentId = $result;
foreach($parentKeys as $key) {
if (!isset($parentId[$key])) {
break;
}
$parentId = $parentId[$key];
}
if (!is_scalar($parentId)) {
$parentId = null;
}
$id = Set::getValue($result, $idKeys);
$parentId = Set::getValue($result, $parentKeys);

if (isset($idMap[$id][$options['children']])) {
$idMap[$id] = array_merge($result, (array)$idMap[$id]);
Expand All @@ -1174,35 +1161,39 @@ public static function nest($data, $options = array()) {
}
}

$root = $return[0];
foreach($parentKeys as $key) {
if (!isset($root[$key])) {
break;
}
$root = $root[$key];
}
if (!is_scalar($root)) {
$root = null;
}
$root = Set::getValue($return[0], $parentKeys);

foreach ($return as $i => $result) {

$parentId = $result;
foreach($parentKeys as $key) {
if (!isset($parentId[$key])) {
break;
}
$parentId = $parentId[$key];
}
if (!is_scalar($parentId)) {
$parentId = null;
}

$parentId = Set::getValue($result, $parentKeys);
if ($parentId != $root) {
unset($return[$i]);
}
}

return $return;
}

/**
* A slimmed down set extract
*
* @param mixed $input an array
* @param mixed $path string or array of array keys
* @return the value at the specified position or null if it doesn't exist
*/
protected static function getValue($input, $path) {
if (is_string($path)) {
$keys = explode('/', trim($path, '/'));
} else {
$keys = $path;
}

$return = $input;
foreach($keys as $key) {
if (!isset($return[$key])) {
return null;
}
$return = $return[$key];
}
return $return;
}
}

0 comments on commit d534013

Please sign in to comment.