diff --git a/src/Neon/Encoder.php b/src/Neon/Encoder.php index 51aa4007..ca9e6fa9 100644 --- a/src/Neon/Encoder.php +++ b/src/Neon/Encoder.php @@ -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; diff --git a/src/Neon/Node/InlineArrayNode.php b/src/Neon/Node/InlineArrayNode.php index 6b19fd31..6618c25b 100644 --- a/src/Neon/Node/InlineArrayNode.php +++ b/src/Neon/Node/InlineArrayNode.php @@ -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]; } } diff --git a/src/Neon/Parser.php b/src/Neon/Parser.php index 46590a78..8438226f 100644 --- a/src/Neon/Parser.php +++ b/src/Neon/Parser.php @@ -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: diff --git a/tests/Neon/Encoder.phpt b/tests/Neon/Encoder.phpt index 50e4c421..3e7c7bd6 100644 --- a/tests/Neon/Encoder.phpt +++ b/tests/Neon/Encoder.phpt @@ -131,12 +131,12 @@ Assert::same( ); Assert::same( - '[]', + '{}', Neon::encode((object) []) ); Assert::same( - '[]', + '{}', Neon::encode(new stdClass) ); diff --git a/tests/Neon/fixtures/Encoder.nodes.txt b/tests/Neon/fixtures/Encoder.nodes.txt index 43060fdd..51492b65 100644 --- a/tests/Neon/fixtures/Encoder.nodes.txt +++ b/tests/Neon/fixtures/Encoder.nodes.txt @@ -1,4 +1,5 @@ Nette\Neon\Node\InlineArrayNode + bracket: '{' items: array (7) | 0 => Nette\Neon\Node\ArrayItemNode | | key: Nette\Neon\Node\LiteralNode @@ -6,6 +7,7 @@ Nette\Neon\Node\InlineArrayNode | | | startPos: null | | | endPos: null | | value: Nette\Neon\Node\InlineArrayNode + | | | bracket: '{' | | | items: array (2) | | | | 0 => Nette\Neon\Node\ArrayItemNode | | | | | key: Nette\Neon\Node\LiteralNode @@ -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 @@ -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 diff --git a/tests/Neon/fixtures/Parser.nodes.neon b/tests/Neon/fixtures/Parser.nodes.neon index bc761a4b..d48ea656 100644 --- a/tests/Neon/fixtures/Parser.nodes.neon +++ b/tests/Neon/fixtures/Parser.nodes.neon @@ -2,7 +2,7 @@ first: - a next: - - {k, l, m: null, n} + - [k, l, m: null, n] second: sub: diff --git a/tests/Neon/fixtures/Parser.nodes.txt b/tests/Neon/fixtures/Parser.nodes.txt index 41df9478..c753a3ba 100644 --- a/tests/Neon/fixtures/Parser.nodes.txt +++ b/tests/Neon/fixtures/Parser.nodes.txt @@ -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