Skip to content

Commit

Permalink
Update documentation and code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 25, 2019
1 parent 783703c commit f75af46
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ It provides different trees implementations:

Exporters and importers:
* **Ascii**: Export a tree into an ascii graphic, just for swag and visualisation fun.
* **Graph**: Export a tree into a graph using the [graphp/graphp](https://github.com/graphp/graph) library.
* **Graph**: Export a tree into a Graph using the [graphp/graphp](https://github.com/graphp/graph) library.
* **GraphViz**: Export a tree into a script in [GraphViz](http://www.graphviz.org/) format.
* **Text**: Export a tree into a simple string, easy for storing in a database.

Modifier:
Expand All @@ -51,8 +52,7 @@ Blog post: [https://not-a-number.io/2018/phptree-a-fast-tree-implementation](htt

## Optional packages

* [graphp/graphp](https://github.com/graphp/graph): To convert a tree into a graph.
* [graphp/graphviz](https://github.com/graphp/graphviz): To render a graph into dot format or an image.
* [graphp/graphp](https://github.com/graphp/graph): To export a tree into a Graph.

## Usage

Expand All @@ -61,8 +61,7 @@ Blog post: [https://not-a-number.io/2018/phptree-a-fast-tree-implementation](htt

declare(strict_types = 1);

use Graphp\GraphViz\GraphViz;
use drupol\phptree\Exporter\Graph;
use drupol\phptree\Exporter\Gv;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\Exporter\Text;

Expand All @@ -79,14 +78,15 @@ foreach (\range('A', 'Z') as $v) {
// Add children to the root node.
$tree->add(...$nodes);

// Export to an image.
$graphViz = new GraphViz();
$graphExporter = new Graph();
$graphViz->display($graphExporter->export($tree));

// Export to text.
$textExporter = new Text();
echo $textExporter->export($tree); // [root[A[C[G[O][P]][H[Q][R]]][D[I[S][T]][J[U][V]]]][B[E[K[W][X]][L[Y][Z]]][F[M][N]]]]⏎

// Export to a GraphViz script.
$exporter = new Gv();
$dotScript = $exporter->export($tree);
file_put_contents('graph.gv', $dotScript);
// Then do "dot -Tsvg graph.gv -o graph.svg" to export the script to SVG.
```

## Code quality, tests and benchmarks
Expand Down
13 changes: 5 additions & 8 deletions src/Exporter/Gv.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class Gv implements ExporterInterface
/**
* The graph attributes.
*
* @var string[]
* @var string[]|string[][]
*/
private $attributes = [];

/**
* Is the graph directed or undirected.
* The graph type.
*
* @var bool
*/
Expand All @@ -31,10 +32,6 @@ class Gv implements ExporterInterface
*/
public function export(NodeInterface $node): string
{
$directed = true === $this->getDirected() ?
'->' :
'--';

$attributes = '';
foreach ($this->attributes as $key => $attribute) {
if (\is_string($attribute)) {
Expand Down Expand Up @@ -72,7 +69,7 @@ public function export(NodeInterface $node): string
$edges .= \sprintf(
' "%s" %s "%s";' . "\n",
$this->getHash($parent),
$directed,
true === $this->getDirected() ? '->' : '--',
$this->getHash($child)
);
}
Expand All @@ -92,7 +89,7 @@ public function getDirected(): bool
}

/**
* Set the graph as directed.
* Set the graph type, directed or undirected.
*
* @param bool $directed
* True for a directed graph, false otherwise.
Expand Down
1 change: 1 addition & 0 deletions src/Node/AttributeNodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface AttributeNodeInterface extends NaryNodeInterface
* {@inheritdoc}
*/
public function getAttribute($key);

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 6 additions & 4 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __clone()
{
$this->storage = clone $this->storage;

/** @var \drupol\phptree\Node\NodeInterface $child */
foreach ($this->children() as $child) {
$child->setParent($this);
}
Expand All @@ -63,15 +64,14 @@ public function all(): \Traversable
{
yield $this;

foreach ($this->children() as $candidate) {
yield from $candidate->all();
/** @var \drupol\phptree\Node\NodeInterface $child */
foreach ($this->children() as $child) {
yield from $child->all();
}
}

/**
* {@inheritdoc}
*
* @return \drupol\phptree\Node\NodeInterface|\Traversable
*/
public function children(): \Traversable
{
Expand Down Expand Up @@ -137,6 +137,7 @@ public function depth(): int
*/
public function find(NodeInterface $node): ?NodeInterface
{
/** @var \drupol\phptree\Node\NodeInterface $candidate */
foreach ($this->all() as $candidate) {
if ($candidate === $node) {
return $node;
Expand Down Expand Up @@ -201,6 +202,7 @@ public function height(): int
{
$height = $this->depth();

/** @var \drupol\phptree\Node\NodeInterface $child */
foreach ($this->children() as $child) {
$height = \max($height, $child->height());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Node/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public function add(NodeInterface ...$node): NodeInterface;
/**
* Get all the nodes of a tree including the parent node itself.
*
* @return \drupol\phptree\Node\NodeInterface|\Traversable
* @return \Traversable
* The node.
*/
public function all(): \Traversable;

/**
* Get the children.
*
* @return \drupol\phptree\Node\NodeInterface|\Traversable
* @return \Traversable
* The children
*/
public function children(): \Traversable;
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface StorageInterface
* @param string $key
* The identifier name of the value.
*
* @return null|int|mixed|string
* @return mixed
* The value.
*/
public function get($key);
Expand Down

0 comments on commit f75af46

Please sign in to comment.