Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typos in README.
Browse files Browse the repository at this point in the history
drupol committed Dec 15, 2018

Unverified

This user has not yet uploaded their public signing key.
1 parent afa6445 commit 1fa55c1
Showing 4 changed files with 151 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/Converter/ConverterInterface.php
Original file line number Diff line number Diff line change
@@ -4,17 +4,21 @@

namespace drupol\phptree\Converter;

use drupol\phptree\Node\NodeInterface;
use drupol\phptree\Node\ValueNodeInterface;

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

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;
@@ -54,11 +55,11 @@ public function getTraverser(): TraverserInterface
}

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

declare(strict_types = 1);

namespace drupol\phptree\Converter;

use drupol\phptree\Node\ValueNodeInterface;

/**
* Class Text
*/
class Text implements ConverterInterface
{
/**
* {@inheritdoc}
*/
public function convert(ValueNodeInterface $node): string
{
$children = [];
/** @var ValueNodeInterface $child */
foreach ($node->children() as $child) {
$children[] = $this->convert($child);
}

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

declare(strict_types = 1);

namespace drupol\phptree\Importer;

use drupol\phptree\Node\NodeInterface;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\Node\ValueNodeInterface;

/**
* Class Text
*/
class Text
{
/**
* {@inheritdoc}
*/
public function import(string $string): NodeInterface
{
$array = $this->parse($string);

return $this->arrayToTree(

);
}

/**
* Convert an array into a tree.
*
* @param array $data
*
* @return \drupol\phptree\Node\ValueNodeInterface
* The tree.
*/
protected function arrayToTree(array $data): ValueNodeInterface
{
$data += [
'children' => [],
];

$node = new ValueNode($data['value']);

foreach ($data['children'] as $key => $child) {
$node->add($this->arrayToTree($child));
}

return $node;
}

/**
* Parse a string into an array.
*
* @param string $string
* The string to parse.
*
* @return array
* The array.
*/
private function parse(string $string): array
{
if (0 === \strpos($string, '[')) {
$string = \substr($string, 1, -1);
}

$current = [];
$length = \strlen($string);
$stack = [];
$buffer_start = null;

for ($position = 0; $position < $length; $position++) {
switch ($string[$position]) {
case '[':
if (null !== $buffer_start) {
$buffer = \substr($string, $buffer_start, $position - $buffer_start);
$buffer_start = null;
$current['value'] = $buffer;
}
$stack['children'][] = $current;
$current = [];

break;
case ']':
if (null !== $buffer_start) {
$buffer = \substr($string, $buffer_start, $position - $buffer_start);
$buffer_start = null;
$current['value'] = $buffer;
}
$t = $current;
$stack += ['children' => []];
$current = \array_pop($stack['children']);
$current['children'][] = $t;

break;
case ' ':
// make each word its own token
if (null !== $buffer_start) {
$buffer = \substr($string, $buffer_start, $position - $buffer_start);
$buffer_start = null;
$current['value'] = $buffer;
}
break;
default:
if (null === $buffer_start) {
$buffer_start = $position;
}
}
}

return $current;
}
}

0 comments on commit 1fa55c1

Please sign in to comment.