nicmart/Tree

Problem with builder : tree are not children

Gounlaf opened this issue · 2 comments

Hi,

I get your Builder example and tried this :

$builder = new \Tree\Builder\NodeBuilder();
$builder->value('A')
    ->leaf('B')
    ->tree('C')
        ->leaf('D')
    ->end()
;

$nodeA = $builder->getNode();
var_dump($nodeA->getChildren());

Result :

array(1) {
  [0] => object(Tree\Node\Node)#291 (3) {
    ["value":"Tree\Node\Node":private] => string(1) "B"
    ["parent":"Tree\Node\Node":private] => object(Tree\Node\Node)#290 (3) {
      ["value":"Tree\Node\Node":private] => string(1) "A"
      ["parent":"Tree\Node\Node":private] => NULL
      ["children":"Tree\Node\Node":private] => *RECURSION*
    }
    ["children":"Tree\Node\Node":private] => array(0) {
    }
  }
}

I build it manyally

$node = new \Tree\Node\Node('A');
$node->setChildren([$b = new \Tree\Node\Node('B'), $c = new \Tree\Node\Node('C')]);
$c->addChild($d = new \Tree\Node\Node('D'));

var_dump($node->getChildren());

And got :

array(2) {
  [0] => object(Tree\Node\Node)#296 (3) {
    ["value":"Tree\Node\Node":private] => string(1) "B"
    ["parent":"Tree\Node\Node":private] => object(Tree\Node\Node)#295 (3) {
      ["value":"Tree\Node\Node":private] => string(1) "A"
      ["parent":"Tree\Node\Node":private] => NULL
      ["children":"Tree\Node\Node":private] => *RECURSION*
    }
    ["children":"Tree\Node\Node":private] => array(0) {
    }
  }
  [1] => object(Tree\Node\Node)#297 (3) {
    ["value":"Tree\Node\Node":private] => string(1) "C"
    ["parent":"Tree\Node\Node":private] => object(Tree\Node\Node)#295 (3) {
      ["value":"Tree\Node\Node":private] => string(1) "A"
      ["parent":"Tree\Node\Node":private] => NULL
      ["children":"Tree\Node\Node":private] => *RECURSION*
    }
    ["children":"Tree\Node\Node":private] => array(1) {
      [0] => object(Tree\Node\Node)#298 (3) {
        ["value":"Tree\Node\Node":private] => string(1) "D"
        ["parent":"Tree\Node\Node":private] => *RECURSION*
        ["children":"Tree\Node\Node":private] => array(0) {
        }
      }
    }
  }
}

In other way, got same result :

$node = new \Tree\Node\Node('A');
$node->addChild($b = new \Tree\Node\Node('B'))
    ->addChild($c = new \Tree\Node\Node('C'));

$c->addChild($d = new \Tree\Node\Node('D'));

var_dump($node->getChildren());


array(2) {
  [0] => object(Tree\Node\Node)#300 (3) {
    ["value":"Tree\Node\Node":private] => string(1) "B"
    ["parent":"Tree\Node\Node":private] => object(Tree\Node\Node)#299 (3) {
      ["value":"Tree\Node\Node":private] => string(1) "A"
      ["parent":"Tree\Node\Node":private] => NULL
      ["children":"Tree\Node\Node":private] => *RECURSION*
    }
    ["children":"Tree\Node\Node":private] => array(0) {
    }
  }
  [1] => object(Tree\Node\Node)#301 (3) {
    ["value":"Tree\Node\Node":private] => string(1) "C"
    ["parent":"Tree\Node\Node":private] => object(Tree\Node\Node)#299 (3) {
      ["value":"Tree\Node\Node":private] => string(1) "A"
      ["parent":"Tree\Node\Node":private] => NULL
      ["children":"Tree\Node\Node":private] => *RECURSION*
    }
    ["children":"Tree\Node\Node":private] => array(1) {
      [0] => object(Tree\Node\Node)#302 (3) {
        ["value":"Tree\Node\Node":private] => string(1) "D"
        ["parent":"Tree\Node\Node":private] => *RECURSION*
        ["children":"Tree\Node\Node":private] => array(0) {
        }
      }
    }
  }
}

I expected that "Builder::tree()" create a leaf, and point to it ;
So, i expected B & C will be children of A with the Builder

What i'm doing wrong ?

Thanks

You find a serious bug in the builder... The tree method was not adding the new subtree to the current node's children!
I've fixed it with c9a2710 and tagged a new release (0.2.2).

Many thanks!

Cool =)
Thanks for fixing 👍