Skip to content

Commit 0b5588a

Browse files
author
Amrouche Hamza
committed
[Serializer] add a context key to return csv always as collection
1 parent 424cbcc commit 0b5588a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
2626
const ENCLOSURE_KEY = 'csv_enclosure';
2727
const ESCAPE_CHAR_KEY = 'csv_escape_char';
2828
const KEY_SEPARATOR_KEY = 'csv_key_separator';
29+
const ALWAYS_COLLECTION = 'csv_always_collection';
2930
const HEADERS_KEY = 'csv_headers';
3031

3132
private $delimiter;
@@ -150,6 +151,10 @@ public function decode($data, $format, array $context = array())
150151
}
151152
fclose($handle);
152153

154+
if ($context[self::ALWAYS_COLLECTION] ?? false) {
155+
return $result;
156+
}
157+
153158
if (empty($result) || isset($result[1])) {
154159
return $result;
155160
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ public function testDecodeCollection()
208208
, 'csv'));
209209
}
210210

211+
public function testDecodeOnlyOneAsCollection()
212+
{
213+
$expected = array(
214+
array('foo' => 'a'),
215+
);
216+
217+
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
218+
foo
219+
a
220+
221+
CSV
222+
, 'csv', array(CsvEncoder::ALWAYS_COLLECTION => true)));
223+
}
224+
211225
public function testDecodeToManyRelation()
212226
{
213227
$expected = array(

0 commit comments

Comments
 (0)