Skip to content

Commit fcbc3c1

Browse files
authored
Merge pull request #2 from drupol/text-converter-importer
Implements a simple text converter and importer.
2 parents afa6445 + 9b87679 commit fcbc3c1

File tree

15 files changed

+331
-37
lines changed

15 files changed

+331
-37
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ It also provides [4 trees traversals algorithm](https://en.wikipedia.org/wiki/Tr
2424
* Pre order
2525
* Breadth first
2626

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

2931
## Documentation
3032

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

5355
use Graphp\GraphViz\GraphViz;
54-
use drupol\phptree\Converter\Graph;
56+
use drupol\phptree\Exporter\Graph;
5557
use drupol\phptree\Node\ValueNode;
5658

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

6870
$graphViz = new GraphViz();
69-
$converter = new Graph();
71+
$exporter = new Graph();
7072

71-
$graphViz->display($converter->convert($tree));
73+
$graphViz->display($exporter->export($tree));
7274
```
7375

7476
## Code quality, tests and benchmarks

benchmarks/AbstractBench.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace drupol\phptree\benchmarks;
6+
7+
abstract class AbstractBench
8+
{
9+
/**
10+
* @return array
11+
*/
12+
public function getData()
13+
{
14+
return \range(1, 100);
15+
}
16+
}

benchmarks/DrupolPhpTreeBench.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace drupol\phptree\benchmarks;
6+
7+
use drupol\phptree\Node\ValueNode;
8+
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
9+
10+
/**
11+
* @Groups({"drupol/phptree"})
12+
* @BeforeMethods({"initObject"})
13+
*/
14+
class DrupolPhpTreeBench extends AbstractBench
15+
{
16+
/**
17+
* @var \drupol\phptree\Node\NodeInterface
18+
*/
19+
private $tree;
20+
21+
/**
22+
* Init the object.
23+
*/
24+
public function initObject()
25+
{
26+
}
27+
28+
/**
29+
* @Revs({1, 100, 1000})
30+
* @Iterations(5)
31+
* @Warmup(10)
32+
*/
33+
public function benchHash()
34+
{
35+
$this->tree = new ValueNode();
36+
37+
foreach ($this->getData() as $value) {
38+
$this->tree->add(new ValueNode($value));
39+
}
40+
}
41+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
},
4646
"autoload-dev": {
4747
"psr-4": {
48-
"drupol\\phptree\\tests\\": "tests/src/"
48+
"drupol\\phptree\\tests\\": "tests/src/",
49+
"drupol\\phptree\\benchmarks\\": "benchmarks/"
4950
}
5051
},
5152
"config": {

spec/drupol/phptree/Converter/GraphSpec.php renamed to spec/drupol/phptree/Exporter/GraphSpec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types = 1);
44

5-
namespace spec\drupol\phptree\Converter;
5+
namespace spec\drupol\phptree\Exporter;
66

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

2929
$this
30-
->convert($root)
30+
->export($root)
3131
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);
3232

3333
$this
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace spec\drupol\phptree\Exporter;
6+
7+
use drupol\phptree\Exporter\Text;
8+
use drupol\phptree\Node\ValueNode;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class TextSpec extends ObjectBehavior
12+
{
13+
public function it_is_initializable()
14+
{
15+
$this->shouldHaveType(Text::class);
16+
}
17+
18+
public function it_can_export_to_text()
19+
{
20+
$tree = new ValueNode('root', 2);
21+
22+
$this
23+
->export($tree)
24+
->shouldReturn('[root]');
25+
26+
$nodes = [];
27+
foreach (\range('A', 'J') as $value) {
28+
$nodes[$value] = new ValueNode($value);
29+
}
30+
31+
$tree->add(...\array_values($nodes));
32+
33+
$this
34+
->export($tree)
35+
->shouldReturn('[root [A [C [G] [H]] [D [I] [J]]] [B [E] [F]]]');
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace spec\drupol\phptree\Importer;
6+
7+
use drupol\phptree\Importer\Text;
8+
use drupol\phptree\Node\ValueNode;
9+
use drupol\phptree\Node\ValueNodeInterface;
10+
use PhpSpec\ObjectBehavior;
11+
12+
class TextSpec extends ObjectBehavior
13+
{
14+
public function it_is_initializable()
15+
{
16+
$this->shouldHaveType(Text::class);
17+
}
18+
19+
public function it_can_import()
20+
{
21+
$string = '[root [A [C [G] [H]] [D [I] [J]]] [B [E] [F]]]';
22+
23+
$tree = new ValueNode('root', 2);
24+
25+
$nodes = [];
26+
foreach (\range('A', 'J') as $value) {
27+
$nodes[$value] = new ValueNode($value);
28+
}
29+
30+
$tree->add(...\array_values($nodes));
31+
32+
$this
33+
->import($string)
34+
->shouldImplement(ValueNodeInterface::class);
35+
36+
$this
37+
->import($string)
38+
->count()
39+
->shouldReturn(10);
40+
41+
$this
42+
->import($string)
43+
->isRoot()
44+
->shouldReturn(TRUE);
45+
}
46+
}

spec/drupol/phptree/tests/Converter/ValueGraphSpec.php renamed to spec/drupol/phptree/tests/Exporter/ValueGraphSpec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
declare(strict_types = 1);
44

5-
namespace spec\drupol\phptree\tests\Converter;
5+
namespace spec\drupol\phptree\tests\Exporter;
66

77
use drupol\phptree\Node\ValueNode;
8-
use drupol\phptree\tests\Converter\ValueGraph;
8+
use drupol\phptree\tests\Exporter\ValueGraph;
99
use PhpSpec\ObjectBehavior;
1010

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

2222
$this
23-
->convert($tree)
23+
->export($tree)
2424
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);
2525
}
2626
}

src/Converter/ConverterInterface.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Exporter/ExporterInterface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace drupol\phptree\Exporter;
6+
7+
use drupol\phptree\Node\ValueNodeInterface;
8+
9+
/**
10+
* Interface ExporterInterface
11+
*/
12+
interface ExporterInterface
13+
{
14+
/**
15+
* Export a node into something.
16+
*
17+
* @param \drupol\phptree\Node\ValueNodeInterface $node
18+
* The node.
19+
*
20+
* @return mixed
21+
* The node exported.
22+
*/
23+
public function export(ValueNodeInterface $node);
24+
}

0 commit comments

Comments
 (0)