diff --git a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php index 1e47d35f17c7f..e887b03d54a06 100644 --- a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php @@ -189,11 +189,36 @@ private function flatten(array $array, array &$result, $keySeparator, $parentKey if (\is_array($value)) { $this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator); } else { - $result[$parentKey.$key] = $value; + $result[$parentKey.$key] = $this->cleanValueByType($value); } } } + /** + * Ensures an actual value is used instead of a blank value when dealing + * with true, false and null (like a nullable bool in a database). + * + * @param string $value + * + * @return string + */ + private function cleanValueByType($value) + { + $clean = $value; + + if (false === $value) { + $clean = 0; + } elseif (null === $value) { + // fputcsv will enclose a space + // https://github.com/php/php-src/blob/master/ext/standard/file.c + $clean = ' '; + } elseif (true === $value) { + $clean = 1; + } + + return $clean; + } + private function getCsvOptions(array $context) { $delimiter = isset($context[self::DELIMITER_KEY]) ? $context[self::DELIMITER_KEY] : $this->delimiter; diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php index 1eb673e8e21ba..a3a13f47f9edf 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php @@ -29,6 +29,25 @@ protected function setUp() $this->encoder = new CsvEncoder(); } + public function testTrueFalseNullValues() + { + $data = [ + 'string' => 'foo', + 'int' => 2, + 'false' => false, + 'true' => true, + 'null' => null, + ]; + + // Check that true, false and null are appropriately handled + $this->assertEquals(<<<'CSV' +string,int,false,true,null +foo,2,0,1," " + +CSV + , $this->encoder->encode($data, 'csv')); + } + public function testSupportEncoding() { $this->assertTrue($this->encoder->supportsEncoding('csv'));