[Serializer] Handle true and false appropriately in CSV encoder

This commit is contained in:
battye 2019-06-13 08:42:28 +00:00 committed by Fabien Potencier
parent 7a6ce5ff85
commit 89cba00c68
2 changed files with 20 additions and 1 deletions

View File

@ -189,7 +189,8 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
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);
}
}
}

View File

@ -29,6 +29,24 @@ class CsvEncoderTest extends TestCase
$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'));