Skip to content

Commit

Permalink
[Serializer] Handle true and false appropriately in CSV encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
battye authored and fabpot committed Jun 14, 2019
1 parent 7a6ce5f commit 89cba00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Expand Up @@ -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);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php
Expand Up @@ -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'));
Expand Down

0 comments on commit 89cba00

Please sign in to comment.