Skip to content

Commit

Permalink
Merge branch 'mdwheele-additional-accessors'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicmart committed Jun 16, 2014
2 parents da19b7a + b7b1ef0 commit c211720
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 9 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ A leaf is a node with no children.
$node->isLeaf();
```

### Getting if the node is a child or not
A child is a node that has a parent.
```php
$node->isChild();
```

### Getting the parent of a node
Reference to the parent node is automatically managed by child-modifiers methods
```php
Expand All @@ -74,6 +80,7 @@ $node->getParent(); // Returns $root
```

### Getting the ancestors of a node

```php
$root = (new Node('root'))
->addChild($child = new Node('child'))
Expand All @@ -83,12 +90,16 @@ $root = (new Node('root'))
$grandchild->getAncestors(); // Returns [$root, $child]
```

#### Related Methods
- `getAncestorsAndSelf` retrieves ancestors of a node with the current node included.

### Getting the root of a node
```php
$root = $node->root();
```

### Getting the neighbors of a node

```php
$root = (new Node('root'))
->addChild($child1 = new Node('child1'))
Expand All @@ -99,6 +110,9 @@ $root = (new Node('root'))
$child2->getNeighbors(); // Returns [$child1, $child3]
```

#### Related Methods
- `getNeighborsAndSelf` retrieves neighbors of current node and the node itself.

## The Builder

The builder provides a convenient way to build trees. It is provided by the ```Builder``` class,
Expand Down
32 changes: 30 additions & 2 deletions src/Tree/Node/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,47 @@ public function setParent(NodeInterface $parent = null);
public function getParent();

/**
* getAncestors
* Retrieves all ancestors of node excluding current node.
*
* @return array
*/
public function getAncestors();

/**
* getNeighbors
* Retrieves all ancestors of node as well as the node itself.
*
* @return Node[]
*/
public function getAncestorsAndSelf();

/**
* Retrieves all neighboring nodes, excluding the current node.
*
* @return array
*/
public function getNeighbors();

/**
* Returns all neighboring nodes, including the current node.
*
* @return Node[]
*/
public function getNeighborsAndSelf();

/**
* Return true if the node is the root, false otherwise
*
* @return bool
*/
public function isRoot();

/**
* Return true if the node is a child, false otherwise.
*
* @return bool
*/
public function isChild();

/**
* Return true if the node has no children, false otherwise
*
Expand Down
41 changes: 34 additions & 7 deletions src/Tree/Node/NodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ public function getAncestors()
return $parents;
}

/**
* {@inheritDoc}
*/
public function getAncestorsAndSelf()
{
return array_merge($this->getAncestors(), [$this]);
}

/**
* {@inheritdoc}
*/
Expand All @@ -161,20 +169,31 @@ public function getNeighbors()
$neighbors = $this->getParent()->getChildren();
$current = $this;

return array_filter(
$neighbors,
function ($item) use ($current) {
return $item != $current;
}
// Uses array_values to reset indexes after filter.
return array_values(
array_filter(
$neighbors,
function ($item) use ($current) {
return $item != $current;
}
)
);
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function getNeighborsAndSelf()
{
return $this->getParent()->getChildren();
}

/**
* {@inheritDoc}
*/
public function isLeaf()
{
return count($this->children) == 0;
return count($this->children) === 0;
}

/**
Expand All @@ -185,6 +204,14 @@ public function isRoot()
return $this->getParent() === null;
}

/**
* {@inheritDoc}
*/
public function isChild()
{
return $this->getParent() !== null;
}

/**
* Find the root of the node
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Tree/Node/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,37 @@ public function testGetAncestors()
$this->assertEquals([$root, $a, $b], $c->getAncestors());
}

public function testGetAncestorsAndSelf()
{
$root = new Node('r');
$root->addChild($a = new Node('a'));
$a->addChild($b = new Node('b'));

$this->assertEquals([$root, $a, $b], $b->getAncestorsAndSelf());
}

public function testGetNeighbors()
{
$root = new Node('r');
$root
->addChild($a = new Node('a'))
->addChild($b = new Node('b'))
->addChild($c = new Node('c'));

$this->assertEquals([$b, $c], $a->getNeighbors());
}

public function testGetNeighborsAndSelf()
{
$root = new Node('r');
$root
->addChild($a = new Node('a'))
->addChild($b = new Node('b'))
->addChild($c = new Node('c'));

$this->assertEquals([$a, $b, $c], $a->getNeighborsAndSelf());
}

public function testIsLeaf()
{
$root = new Node;
Expand Down Expand Up @@ -179,4 +210,13 @@ public function testIsRoot()
$this->assertTrue($root->isRoot());
$this->assertFalse($child->isRoot());
}

public function testIsChild()
{
$root = new Node('root');
$root->addChild($child = new Node('child'));

$this->assertTrue($child->isChild());
$this->assertFalse($root->isChild());
}
}

0 comments on commit c211720

Please sign in to comment.