[Serializer] add a context key to return csv always as collection

This commit is contained in:
Amrouche Hamza 2017-11-30 07:23:16 +01:00
parent 424cbcc1c5
commit d19d05dc5d
No known key found for this signature in database
GPG Key ID: 6968F2785ED4F012
2 changed files with 20 additions and 0 deletions

View File

@ -150,6 +150,10 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
}
fclose($handle);
if ($context['as_collection'] ?? false) {
return $result;
}
if (empty($result) || isset($result[1])) {
return $result;
}

View File

@ -208,6 +208,22 @@ CSV
, 'csv'));
}
public function testDecodeOnlyOneAsCollection()
{
$this->encoder = new CsvEncoder(',', '"', '\\', '.');
$expected = array(
array('foo' => 'a'),
);
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
foo
a
CSV
, 'csv', array('as_collection' => true)));
}
public function testDecodeToManyRelation()
{
$expected = array(