Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 16, 2018
1 parent 1fa55c1 commit b8406f6
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 83 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

declare(strict_types = 1);

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

use drupol\phptree\Node\ValueNodeInterface;

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

declare(strict_types = 1);

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

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

/**
* Export a node into a Graph.
*
* @param \drupol\phptree\Node\ValueNodeInterface $node
* The node.
*
* @return \Fhaculty\Graph\Graph
* The graph.
*/
public function convert(ValueNodeInterface $node): OriginalGraph
public function export(ValueNodeInterface $node): OriginalGraph
{
foreach ($this->getTraverser()->traverse($node) as $node_visited) {
/** @var int $vertexId */
Expand Down
16 changes: 11 additions & 5 deletions src/Converter/Text.php → src/Exporter/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@

declare(strict_types = 1);

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

use drupol\phptree\Node\ValueNodeInterface;

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

return [] === $children ?
Expand Down
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

0 comments on commit b8406f6

Please sign in to comment.