diff --git a/Encoder/CsvEncoder.php b/Encoder/CsvEncoder.php index 1e47d35f17c7..2552e300941c 100644 --- a/Encoder/CsvEncoder.php +++ b/Encoder/CsvEncoder.php @@ -189,7 +189,8 @@ 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; + // Ensures an actual value is used when dealing with true and false + $result[$parentKey.$key] = false === $value ? 0 : (true === $value ? 1 : $value); } } } diff --git a/Tests/Encoder/CsvEncoderTest.php b/Tests/Encoder/CsvEncoderTest.php index 1eb673e8e21b..7b5131cb533a 100644 --- a/Tests/Encoder/CsvEncoderTest.php +++ b/Tests/Encoder/CsvEncoderTest.php @@ -29,6 +29,24 @@ protected function setUp() $this->encoder = new CsvEncoder(); } + public function testTrueFalseValues() + { + $data = [ + 'string' => 'foo', + 'int' => 2, + 'false' => false, + 'true' => true, + ]; + + // Check that true and false are appropriately handled + $this->assertEquals(<<<'CSV' +string,int,false,true +foo,2,0,1 + +CSV + , $this->encoder->encode($data, 'csv')); + } + public function testSupportEncoding() { $this->assertTrue($this->encoder->supportsEncoding('csv'));