10000 [Serializer] Remove CsvEncoder "as_collection" deprecation & change default value by ogizanagi · Pull Request #31684 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Remove CsvEncoder "as_collection" deprecation & change default value #31684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ SecurityBundle
Serializer
----------

* The default value of the `CsvEncoder` "as_collection" option was changed to `true`.
* The `AbstractNormalizer::handleCircularReference()` method has two new `$format` and `$context` arguments.

Translation
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* throw an exception when creating a `Serializer` with normalizers which neither implement `NormalizerInterface` nor `DenormalizerInterface`
* throw an exception when creating a `Serializer` with encoders which neither implement `EncoderInterface` nor `DecoderInterface`
* changed the default value of the `CsvEncoder` "as_collection" option to `true`

4.3.0
-----
Expand Down
12 changes: 5 additions & 7 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface

private $formulasStartCharacters = ['=', '-', '+', '@'];
private $defaultContext = [
self::AS_COLLECTION_KEY => true,
self::DELIMITER_KEY => ',',
self::ENCLOSURE_KEY => '"',
self::ESCAPE_CHAR_KEY => '\\',
Expand Down Expand Up @@ -135,7 +136,7 @@ public function decode($data, $format, array $context = [])
$headerCount = [];
$result = [];

list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);
list($delimiter, $enclosure, $escapeChar, $keySeparator, , , $asCollection) = $this->getCsvOptions($context);

while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
$nbCols = \count($cols);
Expand Down Expand Up @@ -183,18 +184,14 @@ public function decode($data, $format, array $context = [])
}
fclose($handle);

if ($context[self::AS_COLLECTION_KEY] ?? false) {
if ($asCollection) {
return $result;
}

if (empty($result) || isset($result[1])) {
return $result;
}

if (!isset($context['as_collection'])) {
@trigger_error('Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. You should set it to false explicitly instead as true will be the default value in 5.0.', E_USER_DEPRECATED);
}

// If there is only one data line in the document, return it (the line), the result is not considered as a collection
return $result[0];
}
Expand Down Expand Up @@ -233,12 +230,13 @@ private function getCsvOptions(array $context): array
$keySeparator = $context[self::KEY_SEPARATOR_KEY] ?? $this->defaultContext[self::KEY_SEPARATOR_KEY];
$headers = $context[self::HEADERS_KEY] ?? $this->defaultContext[self::HEADERS_KEY];
$escapeFormulas = $context[self::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[self::ESCAPE_FORMULAS_KEY];
$asCollection = $context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY];

if (!\is_array($headers)) {
throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, \gettype($headers)));
}

return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas];
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $asCollection];
}

/**
Expand Down
24 changes: 2 additions & 22 deletions src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,21 +327,6 @@ public function testSupportsDecoding()
$this->assertFalse($this->encoder->supportsDecoding('foo'));
}

/**
* @group legacy
* @expectedDeprecation Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. You should set it to false explicitly instead as true will be the default value in 5.0.
*/
public function testDecodeLegacy()
{
$expected = ['foo' => 'a', 'bar' => 'b'];

$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
foo,bar
a,b
CSV
, 'csv'));
}

public function testDecodeAsSingle()
{
$expected = ['foo' => 'a', 'bar' => 'b'];
Expand Down Expand Up @@ -382,9 +367,7 @@ public function testDecode()
a

CSV
, 'csv', [
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]));
, 'csv'));
}

public function testDecodeToManyRelation()
Expand Down Expand Up @@ -449,9 +432,7 @@ private function doTestDecodeCustomSettings(bool $legacy = false)
a;bar-baz
'hell''o';b;c
CSV
, 'csv', [
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]));
, 'csv'));
}

public function testDecodeCustomSettingsPassedInContext()
Expand All @@ -466,7 +447,6 @@ public function testDecodeCustomSettingsPassedInContext()
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]));
}

Expand Down
0