8000 [Serializer] add a context key to return csv always as collection · symfony/symfony@cbe28c0 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit cbe28c0

Browse files
author
Amrouche Hamza
committed
[Serializer] add a context key to return csv always as collection
1 parent 020d78a commit cbe28c0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

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

156+
if (isset($context['alwaysAsCollection']) && true === $context['alwaysAsCollection']) {
157+
return $result;
158+
}
159+
156160
if (empty($result) || isset($result[1])) {
157161
return $result;
158162
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ public function testDecodeCollection()
178178
, 'csv'));
179179
}
180180

181+
public function testDecodeOnlyOneAsCollection()
182+
{
183+
$expected = array(
184+
array('foo' => 'a'),
185+
);
186+
187+
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
188+
foo
189+
a
190+
191+
CSV
192+
, 'csv', array('alwaysAsCollection' => true)));
193+
}
194+
181195
public function testDecodeToManyRelation()
182196
{
183197
$expected = array(

0 commit comments

Comments
 (0)
0