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

Skip to content

Commit b0bdaab

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

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ public function decode($data, $format, array $context = array())
150150
}
151151
fclose($handle);
152152

153+
if (isset($context['as_collection']) && $context['as_collection']) {
154+
return $result;
155+
}
156+
153157
if (empty($result) || isset($result[1])) {
154158
return $result;
155159
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,22 @@ public function testDecodeCollection()
208208
, 'csv'));
209209
}
210210

211+
public function testDecodeOnlyOneAsCollection()
212+
{
213+
$this->encoder = new CsvEncoder(',', '"', '\\', '.');
214+
215+
$expected = array(
216+
array('foo' => 'a'),
217+
);
218+
219+
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
220+
foo
221+
a
222+
223+
CSV
224+
, 'csv', array('as_collection' => true)));
225+
}
226+
211227
public function testDecodeToManyRelation()
212228
{
213229
$expected = array(

0 commit comments

Comments
 (0)