-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from drupol/text-converter-importer
Implements a simple text converter and importer.
- Loading branch information
Showing
15 changed files
with
331 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]]'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.