8000 feature #29283 [Serializer] CsvEncoder no header option (encode / dec… · symfony/symfony@23cdc73 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23cdc73

Browse files
committed
feature #29283 [Serializer] CsvEncoder no header option (encode / decode) (redecs)
This PR was squashed before being merged into the 4.3-dev branch (closes #29283). Discussion ---------- [Serializer] CsvEncoder no header option (encode / decode) | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27447 | License | MIT | Doc PR | - This PR wants to introduce a new context option for the CsvEncoder, `CsvEncoder::NO_HEADERS_KEY` (boolean), that allows CSV encoding/decoding when you don't have/need a header. By default this is assumed to be false, so the headers are included in the CSV output or assumed to be present in the CSV input. When the option is set to true, the following behaviour occurs. Encoding === The following PHP input ```php array(array('a','b'), array('c', 'd')) ``` will generate this CSV output ```csv a,b c,d ``` Decoding === Considering the CSV input to be ```csv a,b c,d ``` the following PHP array will be returned ```php array ( 0 => array ( 0 => 'a', 1 => 'b', ), 1 => array ( 0 => 'c', 1 => 'd', ), ) ``` Commits ------- 0e63c61 [Serializer] CsvEncoder no header option (encode / decode)
2 parents c4d6d15 + 0e63c61 commit 23cdc73

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
8000 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)
0