From 59638065c0f339a25b3eeb765e35a314d8736ea5 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 26 Mar 2015 18:56:30 +0100 Subject: [PATCH] Encoder: removed trailing spaces [Closes #20] --- src/Neon/Encoder.php | 6 +++--- tests/Neon/Encoder.phpt | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Neon/Encoder.php b/src/Neon/Encoder.php index c47e918f..62266dcb 100644 --- a/src/Neon/Encoder.php +++ b/src/Neon/Encoder.php @@ -53,9 +53,9 @@ public function encode($var, $options = NULL) foreach ($var as $k => $v) { $v = $this->encode($v, self::BLOCK); $s .= ($isList ? '-' : $this->encode($k) . ':') - . (strpos($v, "\n") === FALSE ? ' ' . $v : "\n\t" . str_replace("\n", "\n\t", $v)) - . "\n"; - continue; + . (strpos($v, "\n") === FALSE + ? ' ' . $v . "\n" + : "\n" . preg_replace('#^(?=.)#m', "\t", $v) . (substr($v, -2, 1) === "\n" ? '' : "\n")); } return $s; diff --git a/tests/Neon/Encoder.phpt b/tests/Neon/Encoder.phpt index 8a8c28ae..695f449d 100644 --- a/tests/Neon/Encoder.phpt +++ b/tests/Neon/Encoder.phpt @@ -77,3 +77,8 @@ Assert::same( PHP_VERSION_ID >= 50400 ? '",žlu/ťoučký"' : '",\u017elu\/\u0165ou\u010dk\u00fd"', Neon::encode(',žlu/ťoučký') ); + +Assert::same( + "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", + Neon::encode(array('foo' => 1, 'bar' => array('x' => array(1, 2), 'y' => array(3, 4)), 'baz' => NULL), Neon::BLOCK) +);