Skip to content

Commit 5963806

Browse files
committed
Encoder: removed trailing spaces [Closes #20]
1 parent c14c3ae commit 5963806

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Neon/Encoder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function encode($var, $options = NULL)
5353
foreach ($var as $k => $v) {
5454
$v = $this->encode($v, self::BLOCK);
5555
$s .= ($isList ? '-' : $this->encode($k) . ':')
56-
. (strpos($v, "\n") === FALSE ? ' ' . $v : "\n\t" . str_replace("\n", "\n\t", $v))
57-
. "\n";
58-
continue;
56+
. (strpos($v, "\n") === FALSE
57+
? ' ' . $v . "\n"
58+
: "\n" . preg_replace('#^(?=.)#m', "\t", $v) . (substr($v, -2, 1) === "\n" ? '' : "\n"));
5959
}
6060
return $s;
6161

tests/Neon/Encoder.phpt

+5
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ Assert::same(
7777
PHP_VERSION_ID >= 50400 ? '",žlu/ťoučký"' : '",\u017elu\/\u0165ou\u010dk\u00fd"',
7878
Neon::encode(',žlu/ťoučký')
7979
);
80+
81+
Assert::same(
82+
"foo: 1\nbar:\n\tx:\n\t\t- 1\n\t\t- 2\n\n\ty:\n\t\t- 3\n\t\t- 4\n\nbaz: null\n",
83+
Neon::encode(array('foo' => 1, 'bar' => array('x' => array(1, 2), 'y' => array(3, 4)), 'baz' => NULL), Neon::BLOCK)
84+
);

0 commit comments

Comments
 (0)