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
-------
d19d05dc5d
[Serializer] add a context key to return csv always as collection
This commit is contained in:
commit
e043478ba5
@ -157,6 +157,10 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
|
|||||||
}
|
}
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
|
|
||||||
|
if ($context['as_collection'] ?? false) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($result) || isset($result[1])) {
|
if (empty($result) || isset($result[1])) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
@ -311,6 +311,22 @@ CSV
|
|||||||
, '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()
|
public function testDecodeToManyRelation()
|
||||||
{
|
{
|
||||||
$expected = array(
|
$expected = array(
|
||||||
|
Reference in New Issue
Block a user