Skip to content

Commit

Permalink
Merge pull request #12 from mathiasverraes/getting-leaf-should-return…
Browse files Browse the repository at this point in the history
…-its-value

return the value of a Leaf when it is called or get
  • Loading branch information
mathiasverraes committed Apr 29, 2014
2 parents 342d68d + 65453e2 commit 29e0a62
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/Verraes/MagicTree/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,13 @@ public function remove($key)

public function offsetGet($index)
{
if (!isset($this->_children[$index])) {
$this->addElement($index, new Branch());
}
return $this->_children[$index];
return $this->getChild($index);
}


public function __get($name)
{
if (!isset($this->_children[$name])) {
$this->addElement($name, new Branch());
}

$child = $this->_children[$name];

if ($child instanceof Leaf) {
return $child->getValue();
}

return $child;
return $this->getChild($name);
}

public function offsetExists($offset)
Expand Down Expand Up @@ -255,4 +242,22 @@ public function isEmpty()
}
return true;
}

/**
* @param $index
* @return mixed
*/
private function getChild($index)
{
if (!isset($this->_children[$index])) {
$this->addElement($index, new Branch());
}
$child = $this->_children[$index];

if ($child instanceof Leaf) {
return $child->getValue();
}

return $child;
}
}
18 changes: 18 additions & 0 deletions tests/Verraes/MagicTree/Tests/MagicTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,24 @@ public function it_should_confirm_or_deny_being_empty()

$this->assertFalse($nonEmptyBranch->isEmpty());
}

/**
* @test
*/
public function getting_a_leaf_should_return_its_value()
{
$branch = new Branch();
$branch->integer(5);

$this->assertInternalType('integer', $branch->integer);
$this->assertInternalType('integer', $branch['integer']);

$branch->string("MagicTree Rules");

$this->assertInternalType('string', $branch->string);
$this->assertInternalType('string', $branch['string']);

}
}


0 comments on commit 29e0a62

Please sign in to comment.