-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
98 additions
and
0 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
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 | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\Neon; | ||
|
||
|
||
/** @internal */ | ||
final class Traverser | ||
{ | ||
/** @var callable(Node): void */ | ||
private $callback; | ||
|
||
|
||
public function traverse(Node $node, callable $callback): Node | ||
{ | ||
$this->callback = $callback; | ||
return $this->traverseNode($node); | ||
} | ||
|
||
|
||
private function traverseNode(Node $node): Node | ||
{ | ||
($this->callback)($node); | ||
foreach ($node->getSubNodes() as $subnode) { | ||
$this->traverseNode($subnode); | ||
} | ||
return $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Neon; | ||
use Nette\Neon\Node; | ||
use Nette\Neon\Node\EntityNode; | ||
use Nette\Neon\Node\LiteralNode; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
$visitor = function (Node $node) { | ||
if ($node instanceof EntityNode) { | ||
foreach ($node->attributes as $i => $attr) { | ||
if (!$attr->key) { | ||
$attr->key = new LiteralNode("key$i"); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
$decoder = new Neon\Decoder; | ||
$node = $decoder->parseToNode(' | ||
a: foo(1, 2, 3) | ||
'); | ||
|
||
$traverser = new Neon\Traverser; | ||
$node = $traverser->traverse($node, $visitor); | ||
$value = $node->toValue(); | ||
|
||
Assert::equal([ | ||
'a' => new Nette\Neon\Entity( | ||
'foo', | ||
['key0' => 1, 'key1' => 2, 'key2' => 3] | ||
), | ||
], $value); |