[Serializer] CsvEncoder no header option (encode / decode)

This commit is contained in:
Mihai Nica 2018-11-22 15:34:35 +02:00 committed by Fabien Potencier
parent f2590d196f
commit 0e63c61190
2 changed files with 42 additions and 7 deletions

View File

@ -29,6 +29,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
const HEADERS_KEY = 'csv_headers'; const HEADERS_KEY = 'csv_headers';
const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas'; const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas';
const AS_COLLECTION_KEY = 'as_collection'; const AS_COLLECTION_KEY = 'as_collection';
const NO_HEADERS_KEY = 'no_headers';
private $formulasStartCharacters = array('=', '-', '+', '@'); private $formulasStartCharacters = array('=', '-', '+', '@');
private $defaultContext = array( private $defaultContext = array(
@ -38,6 +39,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
self::ESCAPE_FORMULAS_KEY => false, self::ESCAPE_FORMULAS_KEY => false,
self::HEADERS_KEY => array(), self::HEADERS_KEY => array(),
self::KEY_SEPARATOR_KEY => '.', self::KEY_SEPARATOR_KEY => '.',
self::NO_HEADERS_KEY => false,
); );
/** /**
@ -95,7 +97,9 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers)); $headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar); if (!($context[self::NO_HEADERS_KEY] ?? false)) {
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
}
$headers = array_fill_keys($headers, ''); $headers = array_fill_keys($headers, '');
foreach ($data as $row) { foreach ($data as $row) {
@ -139,13 +143,20 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
if (null === $headers) { if (null === $headers) {
$nbHeaders = $nbCols; $nbHeaders = $nbCols;
foreach ($cols as $col) { if ($context[self::NO_HEADERS_KEY] ?? false) {
$header = explode($keySeparator, $col); for ($i = 0; $i < $nbCols; ++$i) {
$headers[] = $header; $headers[] = array($i);
$headerCount[] = \count($header); }
} $headerCount = array_fill(0, $nbCols, 1);
} else {
foreach ($cols as $col) {
$header = explode($keySeparator, $col);
$headers[] = $header;
$headerCount[] = \count($header);
}
continue; continue;
}
} }
$item = array(); $item = array();

View File

@ -309,6 +309,18 @@ CSV
))); )));
} }
public function testEncodeWithoutHeader()
{
$this->assertSame(<<<'CSV'
a,b
c,d
CSV
, $this->encoder->encode(array(array('a', 'b'), array('c', 'd')), 'csv', array(
CsvEncoder::NO_HEADERS_KEY => true,
)));
}
public function testSupportsDecoding() public function testSupportsDecoding()
{ {
$this->assertTrue($this->encoder->supportsDecoding('csv')); $this->assertTrue($this->encoder->supportsDecoding('csv'));
@ -480,4 +492,16 @@ CSV
{ {
$this->assertEquals(array(), $this->encoder->decode('', 'csv')); $this->assertEquals(array(), $this->encoder->decode('', 'csv'));
} }
public function testDecodeWithoutHeader()
{
$this->assertEquals(array(array('a', 'b'), array('c', 'd')), $this->encoder->decode(<<<'CSV'
a,b
c,d
CSV
, 'csv', array(
CsvEncoder::NO_HEADERS_KEY => true,
)));
}
} }