Skip to content

Commit 7071669

Browse files
committedJun 12, 2015
Refactor variables
1 parent e87e8d8 commit 7071669

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed
 

‎src/Baum/SetMapper.php

+18-21
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,12 @@ public function updateMap($nodeList) {
6868
$result = true;
6969

7070
try {
71-
$flattenTree = $this->flattenNestable($nodeList);
71+
$flattenTree = $self->flattenNestable($nodeList);
7272

73-
foreach ($flattenTree as $branch) {
73+
foreach ($flattenTree as $node) {
7474
$self->node->flushEventListeners();
75-
$model = $self->node->find($branch['id']);
76-
$model->fill($branch);
77-
$model->save();
75+
$model = $self->node->findOrFail(array_get($node, $self->node->getKeyName()));
76+
$model->update($node);
7877
}
7978
} catch (\Exception $e) {
8079
$result = false;
@@ -89,40 +88,38 @@ public function updateMap($nodeList) {
8988
/**
9089
* Flattens an array to contain 'id', 'lft', 'rgt', 'depth', 'parent_id' as a valid tree
9190
* @param $nestableArray
92-
* @param null $parent_id
91+
* @param mixed $parent_id
9392
* @param int $depth
9493
* @param int $bound
9594
* @return array
9695
*/
97-
public function flattenNestable($nestableArray, $parent_id = null, $depth = 0, &$bound = 0)
96+
public function flattenNestable($nodeList, $parent_id = null, $depth = 0, &$bound = 0)
9897
{
99-
$return = array();
100-
101-
foreach ($nestableArray as $subArray) {
102-
$returnSubSubArray = array();
98+
$nodes = array();
10399

100+
foreach ($nodeList as $node) {
104101
$lft = ++$bound;
105102

106-
if (isset($subArray['children'])) {
107-
$returnSubSubArray = $this->flattenNestable($subArray['children'], $subArray['id'], ($depth + 1), $bound);
108-
$rgt = $bound + 1;
109-
++$bound;
110-
} else {
111-
$rgt = ++$bound;
103+
$children = array_get($node, $this->childrenKeyName, []);
104+
105+
if (!empty($children)) {
106+
$children = $this->flattenNestable($children, array_get($node, $this->node->getKeyName()), $depth + 1, $bound);
112107
}
113108

114-
$return[] = array(
115-
$this->node->getKeyName() => $subArray['id'],
109+
$rgt = ++$bound;
110+
111+
$nodes[] = array(
112+
$this->node->getKeyName() => array_get($node, $this->node->getKeyName()),
116113
$this->node->getParentColumnName() => $parent_id,
117114
$this->node->getDepthColumnName() => $depth,
118115
$this->node->getLeftColumnName() => $lft,
119116
$this->node->getRightColumnName() => $rgt,
120117
);
121118

122-
$return = array_merge($return, $returnSubSubArray);
119+
$nodes = array_merge($nodes, $children);
123120
}
124121

125-
return $return;
122+
return $nodes;
126123
}
127124

128125
/**

0 commit comments

Comments
 (0)
Please sign in to comment.