Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e63c61

Browse files
redecsfabpot
authored andcommitted
[Serializer] CsvEncoder no header option (encode / decode)
1 parent f2590d1 commit 0e63c61

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
2929
const HEADERS_KEY = 'csv_headers';
3030
const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas';
3131
const AS_COLLECTION_KEY = 'as_collection';
32+
const NO_HEADERS_KEY = 'no_headers';
3233

3334
private $formulasStartCharacters = array('=', '-', '+', '@');
3435
private $defaultContext = array(
@@ -38,6 +39,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3839
self::ESCAPE_FORMULAS_KEY => false,
3940
self::HEADERS_KEY => array(),
4041
self::KEY_SEPARATOR_KEY => '.',
42+
self::NO_HEADERS_KEY => false,
4143
);
4244

4345
/**
@@ -95,7 +97,9 @@ public function encode($data, $format, array $context = array())
9597

9698
$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));
9799

98-
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
100+
if (!($context[self::NO_HEADERS_KEY] ?? false)) {
101+
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
102+
}
99103

100104
$headers = array_fill_keys($headers, '');
101105
foreach ($data as $row) {
@@ -139,13 +143,20 @@ public function decode($data, $format, array $context = array())
139143
if (null === $headers) {
140144
$nbHeaders = $nbCols;
141145

142-
foreach ($cols as $col) {
143-
$header = explode($keySeparator, $col);
144-
$headers[] = $header;
145-
$headerCount[] = \count($header);
146-
}
146+
if ($context[self::NO_HEADERS_KEY] ?? false) {
147+
for ($i = 0; $i < $nbCols; ++$i) {
148+
$headers[] = array($i);
149+
}
150+
$headerCount = array_fill(0, $nbCols, 1);
151+
} else {
152+
foreach ($cols as $col) {
153+
$header = explode($keySeparator, $col);
154+
$headers[] = $header;
155+
$headerCount[] = \count($header);
156+
}
147157

148-
continue;
158+
continue;
159+
}
149160
}
150161

151162
$item = array();

src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ public function testEncodeFormulasWithSettingsPassedInContext()
309309
)));
310310
}
311311

312+
public function testEncodeWithoutHeader()
313+
{
314+
$this->assertSame(<<<'CSV'
315+
a,b
316+
c,d
317+
318+
CSV
319+
, $this->encoder->encode(array(array('a', 'b'), array('c', 'd')), 'csv', array(
320+
CsvEncoder::NO_HEADERS_KEY => true,
321+
)));
322+
}
323+
312324
public function testSupportsDecoding()
313325
{
314326
$this->assertTrue($this->encoder->supportsDecoding('csv'));
@@ -480,4 +492,16 @@ public function testDecodeEmptyArray()
480492
{
481493
$this->assertEquals(array(), $this->encoder->decode('', 'csv'));
482494
}
495+
496+
public function testDecodeWithoutHeader()
497+
{
498+
$this->assertEquals(array(array('a', 'b'), array('c', 'd')), $this->encoder->decode(<<<'CSV'
499+
a,b
500+
c,d
501+
502+
CSV
503+
, 'csv', array(
504+
CsvEncoder::NO_HEADERS_KEY => true,
505+
)));
506+
}
483507
}

0 commit comments

Comments
 (0)