Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements a simple text converter and importer. #2

Merged
merged 5 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ It also provides [4 trees traversals algorithm](https://en.wikipedia.org/wiki/Tr
* Pre order
* Breadth first

And it provides 1 converter for the [graphp/graphp](https://github.com/graphp/graph) library.
And it provides Exporters and Importers for:
* Graph: Export a tree into a graph using the [graphp/graphp](https://github.com/graphp/graph) library.
* Text: Export a tree into a simple string.

## Documentation

Expand All @@ -51,7 +53,7 @@ API documentation is automatically generated with [APIGen](https://github.com/Ap
declare(strict_types = 1);

use Graphp\GraphViz\GraphViz;
use drupol\phptree\Converter\Graph;
use drupol\phptree\Exporter\Graph;
use drupol\phptree\Node\ValueNode;

include './vendor/autoload.php';
Expand All @@ -66,9 +68,9 @@ foreach (\range('A', 'Z') as $v) {
$tree->add(...$nodes);

$graphViz = new GraphViz();
$converter = new Graph();
$exporter = new Graph();

$graphViz->display($converter->convert($tree));
$graphViz->display($exporter->export($tree));
```

## Code quality, tests and benchmarks
Expand Down
16 changes: 16 additions & 0 deletions benchmarks/AbstractBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\benchmarks;

abstract class AbstractBench
{
/**
* @return array
*/
public function getData()
{
return \range(1, 100);
}
}
41 changes: 41 additions & 0 deletions benchmarks/DrupolPhpTreeBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\benchmarks;

use drupol\phptree\Node\ValueNode;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;

/**
* @Groups({"drupol/phptree"})
* @BeforeMethods({"initObject"})
*/
class DrupolPhpTreeBench extends AbstractBench
{
/**
* @var \drupol\phptree\Node\NodeInterface
*/
private $tree;

/**
* Init the object.
*/
public function initObject()
{
}

/**
* @Revs({1, 100, 1000})
* @Iterations(5)
* @Warmup(10)
*/
public function benchHash()
{
$this->tree = new ValueNode();

foreach ($this->getData() as $value) {
$this->tree->add(new ValueNode($value));
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
},
"autoload-dev": {
"psr-4": {
"drupol\\phptree\\tests\\": "tests/src/"
"drupol\\phptree\\tests\\": "tests/src/",
"drupol\\phptree\\benchmarks\\": "benchmarks/"
}
},
"config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types = 1);

namespace spec\drupol\phptree\Converter;
namespace spec\drupol\phptree\Exporter;

use drupol\phptree\Converter\Graph;
use drupol\phptree\Exporter\Graph;
use drupol\phptree\Node\Node;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\Traverser\BreadthFirst;
Expand All @@ -27,7 +27,7 @@ public function it_can_generate_a_graph()
->add($child1, $child2, $child3);

$this
->convert($root)
->export($root)
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);

$this
Expand Down
37 changes: 37 additions & 0 deletions spec/drupol/phptree/Exporter/TextSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

namespace spec\drupol\phptree\Exporter;

use drupol\phptree\Exporter\Text;
use drupol\phptree\Node\ValueNode;
use PhpSpec\ObjectBehavior;

class TextSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(Text::class);
}

public function it_can_export_to_text()
{
$tree = new ValueNode('root', 2);

$this
->export($tree)
->shouldReturn('[root]');

$nodes = [];
foreach (\range('A', 'J') as $value) {
$nodes[$value] = new ValueNode($value);
}

$tree->add(...\array_values($nodes));

$this
->export($tree)
->shouldReturn('[root [A [C [G] [H]] [D [I] [J]]] [B [E] [F]]]');
}
}
46 changes: 46 additions & 0 deletions spec/drupol/phptree/Importer/TextSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

namespace spec\drupol\phptree\Importer;

use drupol\phptree\Importer\Text;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\Node\ValueNodeInterface;
use PhpSpec\ObjectBehavior;

class TextSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(Text::class);
}

public function it_can_import()
{
$string = '[root [A [C [G] [H]] [D [I] [J]]] [B [E] [F]]]';

$tree = new ValueNode('root', 2);

$nodes = [];
foreach (\range('A', 'J') as $value) {
$nodes[$value] = new ValueNode($value);
}

$tree->add(...\array_values($nodes));

$this
->import($string)
->shouldImplement(ValueNodeInterface::class);

$this
->import($string)
->count()
->shouldReturn(10);

$this
->import($string)
->isRoot()
->shouldReturn(TRUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types = 1);

namespace spec\drupol\phptree\tests\Converter;
namespace spec\drupol\phptree\tests\Exporter;

use drupol\phptree\Node\ValueNode;
use drupol\phptree\tests\Converter\ValueGraph;
use drupol\phptree\tests\Exporter\ValueGraph;
use PhpSpec\ObjectBehavior;

class ValueGraphSpec extends ObjectBehavior
Expand All @@ -20,7 +20,7 @@ public function it_can_be_extended()
$tree = new ValueNode('root');

$this
->convert($tree)
->export($tree)
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);
}
}
20 changes: 0 additions & 20 deletions src/Converter/ConverterInterface.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Exporter/ExporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\Exporter;

use drupol\phptree\Node\ValueNodeInterface;

/**
* Interface ExporterInterface
*/
interface ExporterInterface
{
/**
* Export a node into something.
*
* @param \drupol\phptree\Node\ValueNodeInterface $node
* The node.
*
* @return mixed
* The node exported.
*/
public function export(ValueNodeInterface $node);
}
13 changes: 9 additions & 4 deletions src/Converter/Graph.php → src/Exporter/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types = 1);

namespace drupol\phptree\Converter;
namespace drupol\phptree\Exporter;

use drupol\phptree\Node\NodeInterface;
use drupol\phptree\Node\ValueNodeInterface;
use drupol\phptree\Traverser\BreadthFirst;
use drupol\phptree\Traverser\TraverserInterface;
use Fhaculty\Graph\Graph as OriginalGraph;
Expand All @@ -13,7 +14,7 @@
/**
* Class Graph
*/
class Graph implements ConverterInterface
class Graph implements ExporterInterface
{
/**
* @var \Fhaculty\Graph\Graph
Expand Down Expand Up @@ -54,11 +55,15 @@ public function getTraverser(): TraverserInterface
}

/**
* @param \drupol\phptree\Node\NodeInterface $node
* Export a node into a Graph.
*
* @param \drupol\phptree\Node\ValueNodeInterface $node
* The node.
*
* @return \Fhaculty\Graph\Graph
* The graph.
*/
public function convert(NodeInterface $node): OriginalGraph
public function export(ValueNodeInterface $node): OriginalGraph
{
foreach ($this->getTraverser()->traverse($node) as $node_visited) {
/** @var int $vertexId */
Expand Down
35 changes: 35 additions & 0 deletions src/Exporter/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\Exporter;

use drupol\phptree\Node\ValueNodeInterface;

/**
* Class Text
*/
class Text implements ExporterInterface
{
/**
* Export the node in a string.
*
* @param \drupol\phptree\Node\ValueNodeInterface $node
* The node.
*
* @return string
* The string.
*/
public function export(ValueNodeInterface $node): string
{
$children = [];
/** @var ValueNodeInterface $child */
foreach ($node->children() as $child) {
$children[] = $this->export($child);
}

return [] === $children ?
\sprintf('[%s]', $node->getValue()):
\sprintf('[%s %s]', $node->getValue(), \implode(' ', $children));
}
}
24 changes: 24 additions & 0 deletions src/Importer/ImporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\Importer;

use drupol\phptree\Node\NodeInterface;

/**
* Interface ImporterInterface
*/
interface ImporterInterface
{
/**
* Import data into a node.
*
* @param mixed $data
* The data to import.
*
* @return NodeInterface
* The new node.
*/
public function import($data): NodeInterface;
}
Loading