From 0e63c61190f706e8a8723433ad44980ab0d9e5e2 Mon Sep 17 00:00:00 2001 From: Mihai Nica Date: Thu, 22 Nov 2018 15:34:35 +0200 Subject: [PATCH] [Serializer] CsvEncoder no header option (encode / decode) --- .../Serializer/Encoder/CsvEncoder.php | 25 +++++++++++++------ .../Tests/Encoder/CsvEncoderTest.php | 24 ++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php index c453120fc6..d751e21046 100644 --- a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php @@ -29,6 +29,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface const HEADERS_KEY = 'csv_headers'; const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas'; const AS_COLLECTION_KEY = 'as_collection'; + const NO_HEADERS_KEY = 'no_headers'; private $formulasStartCharacters = array('=', '-', '+', '@'); private $defaultContext = array( @@ -38,6 +39,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface self::ESCAPE_FORMULAS_KEY => false, self::HEADERS_KEY => array(), 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)); - fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar); + if (!($context[self::NO_HEADERS_KEY] ?? false)) { + fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar); + } $headers = array_fill_keys($headers, ''); foreach ($data as $row) { @@ -139,13 +143,20 @@ class CsvEncoder implements EncoderInterface, DecoderInterface if (null === $headers) { $nbHeaders = $nbCols; - foreach ($cols as $col) { - $header = explode($keySeparator, $col); - $headers[] = $header; - $headerCount[] = \count($header); - } + if ($context[self::NO_HEADERS_KEY] ?? false) { + for ($i = 0; $i < $nbCols; ++$i) { + $headers[] = array($i); + } + $headerCount = array_fill(0, $nbCols, 1); + } else { + foreach ($cols as $col) { + $header = explode($keySeparator, $col); + $headers[] = $header; + $headerCount[] = \count($header); + } - continue; + continue; + } } $item = array(); diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php index 65ced222a9..86ea75b9ea 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php @@ -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() { $this->assertTrue($this->encoder->supportsDecoding('csv')); @@ -480,4 +492,16 @@ 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, + ))); + } }