Skip to content

Commit

Permalink
InlineArrayNode: added property $bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 25, 2021
1 parent 53e7978 commit b195261
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/Neon/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public function valueToNode($val, bool $blockMode = false): Node
);

} elseif (is_object($val) || is_array($val)) {
$node = $blockMode ? new Node\BlockArrayNode : new Node\InlineArrayNode;
if ($blockMode) {
$node = new Node\BlockArrayNode;
} else {
$isList = is_array($val) && (!$val || array_keys($val) === range(0, count($val) - 1));
$node = new Node\InlineArrayNode($isList ? '[' : '{');
}
$node->items = $this->arrayToNodes($val, $blockMode);
return $node;

Expand Down
13 changes: 9 additions & 4 deletions src/Neon/Node/InlineArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
/** @internal */
final class InlineArrayNode extends ArrayNode
{
public function __construct(int $pos = null)
/** @var string */
public $bracket;


public function __construct(string $bracket, int $pos = null)
{
$this->bracket = $bracket;
$this->startPos = $this->endPos = $pos;
}


public function toString(): string
{
$isList = !array_filter($this->items, function ($item) { return $item->key; });
$res = ArrayItemNode::itemsToInlineString($this->items);
return ($isList ? '[' : '{') . $res . ($isList ? ']' : '}');
return $this->bracket
. ArrayItemNode::itemsToInlineString($this->items)
. ['[' => ']', '{' => '}', '(' => ')'][$this->bracket];
}
}
2 changes: 1 addition & 1 deletion src/Neon/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private function parseBraces(): Node\InlineArrayNode
{
$token = $this->tokens->consume();
$endBrace = ['[' => ']', '{' => '}', '(' => ')'][$token->value];
$res = new Node\InlineArrayNode($this->tokens->getPos() - 1);
$res = new Node\InlineArrayNode($token->value, $this->tokens->getPos() - 1);
$keyCheck = [];

loop:
Expand Down
4 changes: 2 additions & 2 deletions tests/Neon/Encoder.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ Assert::same(
);

Assert::same(
'[]',
'{}',
Neon::encode((object) [])
);

Assert::same(
'[]',
'{}',
Neon::encode(new stdClass)
);

Expand Down
4 changes: 4 additions & 0 deletions tests/Neon/fixtures/Encoder.nodes.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Nette\Neon\Node\InlineArrayNode
bracket: '{'
items: array (7)
| 0 => Nette\Neon\Node\ArrayItemNode
| | key: Nette\Neon\Node\LiteralNode
| | | value: 'map'
| | | startPos: null
| | | endPos: null
| | value: Nette\Neon\Node\InlineArrayNode
| | | bracket: '{'
| | | items: array (2)
| | | | 0 => Nette\Neon\Node\ArrayItemNode
| | | | | key: Nette\Neon\Node\LiteralNode
Expand Down Expand Up @@ -39,6 +41,7 @@ Nette\Neon\Node\InlineArrayNode
| | | startPos: null
| | | endPos: null
| | value: Nette\Neon\Node\InlineArrayNode
| | | bracket: '['
| | | items: array (3)
| | | | 0 => Nette\Neon\Node\ArrayItemNode
| | | | | key: null
Expand Down Expand Up @@ -74,6 +77,7 @@ Nette\Neon\Node\InlineArrayNode
| | | startPos: null
| | | endPos: null
| | value: Nette\Neon\Node\InlineArrayNode
| | | bracket: '{'
| | | items: array (4)
| | | | 0 => Nette\Neon\Node\ArrayItemNode
| | | | | key: null
Expand Down
2 changes: 1 addition & 1 deletion tests/Neon/fixtures/Parser.nodes.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ first:
- a

next:
- {k, l, m: null, n}
- [k, l, m: null, n]

second:
sub:
Expand Down
1 change: 1 addition & 0 deletions tests/Neon/fixtures/Parser.nodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Nette\Neon\Node\BlockArrayNode
| | | | | | | '[k,\n
| | | | | | | \t \t l, m:\n
| | | | | | | \t n]'
| | | | | | bracket: '['
| | | | | | items: array (4) ...
| | | | | | startPos: unset
| | | | | | endPos: unset
Expand Down

0 comments on commit b195261

Please sign in to comment.