Skip to content

Commit 89cba00

Browse files
battyefabpot
authored andcommitted
[Serializer] Handle true and false appropriately in CSV encoder
1 parent 7a6ce5f commit 89cba00

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ private function flatten(array $array, array &$result, $keySeparator, $parentKey
189189
if (\is_array($value)) {
190190
$this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator);
191191
} else {
192-
$result[$parentKey.$key] = $value;
192+
// Ensures an actual value is used when dealing with true and false
193+
$result[$parentKey.$key] = false === $value ? 0 : (true === $value ? 1 : $value);
193194
}
194195
}
195196
}

src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ protected function setUp()
2929
$this->encoder = new CsvEncoder();
3030
}
3131

32+
public function testTrueFalseValues()
33+
{
34+
$data = [
35+
'string' => 'foo',
36+
'int' => 2,
37+
'false' => false,
38+
'true' => true,
39+
];
40+
41+
// Check that true and false are appropriately handled
42+
$this->assertEquals(<<<'CSV'
43+
string,int,false,true
44+
foo,2,0,1
45+
46+
CSV
47+
, $this->encoder->encode($data, 'csv'));
48+
}
49+
3250
public function testSupportEncoding()
3351
{
3452
$this->assertTrue($this->encoder->supportsEncoding('csv'));

0 commit comments

Comments
 (0)