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

This commit is contained in:
Maxime Steinhausser 2019-05-29 11:49:47 +02:00
parent c6a077cdde
commit 22dd071b03
4 changed files with 9 additions and 29 deletions

View File

@ -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

View File

@ -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
-----

View File

@ -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 => '\\',
@ -135,7 +136,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
$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);
@ -183,7 +184,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
}
fclose($handle);
if ($context[self::AS_COLLECTION_KEY] ?? false) {
if ($asCollection) {
return $result;
}
@ -191,10 +192,6 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
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];
}
@ -233,12 +230,13 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
$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];
}
/**

View File

@ -327,21 +327,6 @@ CSV
$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'];
@ -382,9 +367,7 @@ foo
a
CSV
, 'csv', [
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]));
, 'csv'));
}
public function testDecodeToManyRelation()
@ -449,9 +432,7 @@ CSV
a;bar-baz
'hell''o';b;c
CSV
, 'csv', [
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]));
, 'csv'));
}
public function testDecodeCustomSettingsPassedInContext()
@ -466,7 +447,6 @@ CSV
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]));
}