Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 855993b

Browse filesBrowse files
author
Amrouche Hamza
committed
[Serializer] add a context key to return csv always as collection
1 parent 424cbcc commit 855993b
Copy full SHA for 855993b

File tree

2 files changed

+19
-0
lines changed
Filter options

2 files changed

+19
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
+5Lines changed: 5 additions & 0 deletions
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 = '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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php
+14Lines changed: 14 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.