8000 bug #32007 [Serializer] Handle true and false appropriately in CSV en… · symfony/symfony@b8978bd · GitHub
[go: up one dir, main page]

Skip to content

Commit b8978bd

Browse files
committed
bug #32007 [Serializer] Handle true and false appropriately in CSV encoder (battye)
This PR was squashed before being merged into the 3.4 branch (closes #32007). Discussion ---------- [Serializer] Handle true and false appropriately in CSV encoder | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27642 | License | MIT | Doc PR | - Previously, if `true` was passed in as a value to the CSV encoder then `fputcsv()` would correctly treat it as 1. However, if `false` was passed in, it would be treated as a blank value. `null` would also be treated as a blank value. This fix makes it consistent so that true and false will map to 1 and 0, while null maps to an empty string. Commits ------- 89cba00 [Serializer] Handle true and false appropriately in CSV encoder
2 parents bd9d0a4 + 89cba00 commit b8978bd

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)
0