- Sponsor
-
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.
Showing
4 changed files
with
151 additions
and
5 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
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,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)); | ||
} | ||
} |
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,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; | ||
} | ||
} |