8000 pass CSV headers via context to CsvEncoder · symfony/symfony@3c2aa91 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c2aa91

Browse files
author
Oliver Hoff
committed
pass CSV headers via context to CsvEncoder
1 parent 3d5ee2e commit 3c2aa91

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
2525
const ENCLOSURE_KEY = 'csv_enclosure';
2626
const ESCAPE_CHAR_KEY = 'csv_escape_char';
2727
const KEY_SEPARATOR_KEY = 'csv_key_separator';
28+
const HEADERS_KEY = 'csv_headers';
2829

2930
private $delimiter;
3031
private $enclosure;
@@ -69,7 +70,7 @@ public function encode($data, $format, array $context = array())
6970
}
7071
}
7172

72-
list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);
73+
list($delimiter, $enclosure, $escapeChar, $keySeparator, $headers) = $this->getCsvOptions($context);
7374

7475
foreach ($data as &$value) {
7576
$flattened = array();
@@ -78,7 +79,7 @@ public function encode($data, $format, array $context = array())
7879
}
7980
unset($value);
8081

81-
$headers = $this->extractHeaders($data);
82+
$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));
8283

8384
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
8485

@@ -195,8 +196,13 @@ protected function getCsvOptions(array $context)
195196
$enclosure = isset($context[self::ENCLOSURE_KEY]) ? $context[self::ENCLOSURE_KEY] : $this->enclosure;
196197
$escapeChar = isset($context[self::ESCAPE_CHAR_KEY]) ? $context[self::ESCAPE_CHAR_KEY] : $this->escapeChar;
197198
$keySeparator = isset($context[self::KEY_SEPARATOR_KEY]) ? $context[self::KEY_SEPARATOR_KEY] : $this->keySeparator;
199+
$headers = isset($context[self::HEADERS_KEY]) ? $context[self::HEADERS_KEY] : array();
200+
201+
if (!is_array($headers)) {
202+
throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, gettype($headers)));
203+
}
198204

199-
return array($delimiter, $enclosure, $escapeChar, $keySeparator);
205+
return array($delimiter, $enclosure, $escapeChar, $keySeparator, $headers);
200206
}
201207

202208
/**

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ public function testEncodeVariableStructure()
153153
$this->assertEquals($csv, $this->encoder->encode($value, 'csv'));
154154
}
155155

156+
public function testEncodeCustomHeaders()
157+
{
158+
$context = array(
159+
CsvEncoder::HEADERS_KEY => array(
160+
'b',
161+
'c',
162+
),
163+
);
164+
$value = array(
165+
array('a' => 'foo', 'b' => 'bar'),
166+
);
167+
$csv = <<<CSV
168+
b,c,a
169+
bar,,foo
170+
171+
CSV;
172+
173+
$this->assertEquals($csv, $this->encoder->encode($value, 'csv', $context));
174+
}
175+
156176
public function testSupportsDecoding()
157177
{
158178
$this->assertTrue($this->encoder->supportsDecoding('csv'));

0 commit comments

Comments
 (0)
0