Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serializer] Handle true and false appropriately in CSV encoder #32007

Merged
merged 1 commit into from Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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