8000 feature #25218 [Serializer] add a constructor arguement to return csv… · symfony/symfony@e043478 · GitHub
[go: up one dir, main page]

Skip to content

Commit e043478

Browse files
committed
feature #25218 [Serializer] add a constructor arguement to return csv always as collection (Simperfit)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Serializer] add a constructor arguement to return csv always as collection | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #21616 | License | MIT | Doc PR | TODO create a doc PR for the 3 ways of getting csv collection, or a single Coding in the train again ;). ![img_9980](https://user-images.githubusercontent.com/3451634/33417042-f13063e4-d59f-11e7-8f30-143da768b1d7.JPG) This is to be able to add a new behaviour to the csv encoder when passing the alwaysAsCollection context key, this will return a collection even if there is only one element. Commits ------- d19d05d [Serializer] add a context key to return csv always as collection
2 parents 81e19e8 + d19d05d commit e043478

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
@@ -157,6 +157,10 @@ public function decode($data, $format, array $context = array())
157157
}
158158
fclose($handle);
159159

160+
if ($context['as_collection'] ?? false) {
161+
return $result;
162+
}
163+
160164
if (empty($result) || isset($result[1])) {
161165
return $result;
162166
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,22 @@ public function testDecodeCollection()
311311
, 'csv'));
312312
}
313313

314+
public function testDecodeOnlyOneAsCollection()
315+
{
316+
$this->encoder = new CsvEncoder(',', '"', '\\', '.');
317+
318+
$expected = array(
319+
array('foo' => 'a'),
320+
);
321+
322+
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
323+
foo
324+
a
325+
326+
CSV
327+
, 'csv', array('as_collection' => true)));
328+
}
329+
314330
public function testDecodeToManyRelation()
315331
{
316332
$expected = array(

0 commit comments

Comments
 (0)
0