Skip to content

[Serializer] Allow to pass csv encoder options in context #22537

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

Merged
merged 1 commit into from
Apr 29, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
class CsvEncoder implements EncoderInterface, DecoderInterface
{
const FORMAT = 'csv';
const DELIMITER_KEY = 'csv_delimiter';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking: is there any reason to name these constants *_KEY instead of *_CHAR? I was checking the popular CSV library published by ThePhpLeague and they say that the separator, enclosure, etc. can only be 1 char: https://github.com/thephpleague/csv/blob/master/src/AbstractCsv.php#L42

Copy link
Contributor Author

@ogizanagi ogizanagi Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the KEY suffix is not about csv at all. It's about the constant representing the key of the option to pass in the context argument. See DateTimeNormalizer::FORMAT_KEY for reference. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my confusion!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps your confusion is legit and the naming should be more explicit though. I named it after DateTimeNormalizer::FORMAT_KEY, but that's currently the only occurence of such a suffixed constant in the serializer component. Other constants for options like in the AbstractNormalizer are simply named after the option itself, without any suffix.

Also in a different topic, some options like allow_extra_attributes, attributes, or key_type don't even have any constant. Maybe it's worth adding them and always ask for constants for new options in the future?

ping @dunglas.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in a different topic, some options like allow_extra_attributes, attributes, or key_type don't even have any constant. Maybe it's worth adding them and always ask for constants for new options in the future?

I definitely agree.

const ENCLOSURE_KEY = 'csv_enclosure';
const ESCAPE_CHAR_KEY = 'csv_escape_char';
const KEY_SEPARATOR_KEY = 'csv_key_separator';

private $delimiter;
private $enclosure;
Expand Down Expand Up @@ -65,19 +69,21 @@ public function encode($data, $format, array $context = array())
}
}

list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);

$headers = null;
foreach ($data as $value) {
$result = array();
$this->flatten($value, $result);
$this->flatten($value, $result, $keySeparator);

if (null === $headers) {
$headers = array_keys($result);
fputcsv($handle, $headers, $this->delimiter, $this->enclosure, $this->escapeChar);
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
} elseif (array_keys($result) !== $headers) {
throw new InvalidArgumentException('To use the CSV encoder, each line in the data array must have the same structure. You may want to use a custom normalizer class to normalize the data format before passing it to the CSV encoder.');
}

fputcsv($handle, $result, $this->delimiter, $this->enclosure, $this->escapeChar);
fputcsv($handle, $result, $delimiter, $enclosure, $escapeChar);
}

rewind($handle);
Expand Down Expand Up @@ -108,14 +114,16 @@ public function decode($data, $format, array $context = array())
$nbHeaders = 0;
$result = array();

while (false !== ($cols = fgetcsv($handle, 0, $this->delimiter, $this->enclosure, $this->escapeChar))) {
list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);

while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
$nbCols = count($cols);

if (null === $headers) {
$nbHeaders = $nbCols;

foreach ($cols as $col) {
$headers[] = explode($this->keySeparator, $col);
$headers[] = explode($keySeparator, $col);
}

continue;
Expand Down Expand Up @@ -166,16 +174,27 @@ public function supportsDecoding($format)
*
* @param array $array
* @param array $result
* @param string $keySeparator
* @param string $parentKey
*/
private function flatten(array $array, array &$result, $parentKey = '')
private function flatten(array $array, array &$result, $keySeparator, $parentKey = '')
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$this->flatten($value, $result, $parentKey.$key.$this->keySeparator);
$this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator);
} else {
$result[$parentKey.$key] = $value;
}
}
}

private function getCsvOptions(array $context)
{
$delimiter = isset($context[self::DELIMITER_KEY]) ? $context[self::DELIMITER_KEY] : $this->delimiter;
$enclosure = isset($context[self::ENCLOSURE_KEY]) ? $context[self::ENCLOSURE_KEY] : $this->enclosure;
$escapeChar = isset($context[self::ESCAPE_CHAR_KEY]) ? $context[self::ESCAPE_CHAR_KEY] : $this->escapeChar;
$keySeparator = isset($context[self::KEY_SEPARATOR_KEY]) ? $context[self::KEY_SEPARATOR_KEY] : $this->keySeparator;

return array($delimiter, $enclosure, $escapeChar, $keySeparator);
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public function testEncodeCustomSettings()
, $this->encoder->encode($value, 'csv'));
}

public function testEncodeCustomSettingsPassedInContext()
{
$value = array('a' => 'he\'llo', 'c' => array('d' => 'foo'));

$this->assertSame(<<<'CSV'
a;c-d
'he''llo';foo

CSV
, $this->encoder->encode($value, 'csv', array(
CsvEncoder::DELIMITER_KEY => ';',
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
)));
}

public function testEncodeEmptyArray()
{
$this->assertEquals("\n\n", $this->encoder->encode(array(), 'csv'));
Expand Down Expand Up @@ -207,6 +224,21 @@ public function testDecodeCustomSettings()
, 'csv'));
}

public function testDecodeCustomSettingsPassedInContext()
{
$expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b'));
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
a;bar-baz
'hell''o';b;c
CSV
, 'csv', array(
CsvEncoder::DELIMITER_KEY => ';',
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
)));
}

public function testDecodeMalformedCollection()
{
$expected = array(
Expand Down