diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 74c8bd4580..e6222abe89 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -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 diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 9cdb2243be..4b05d8af94 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php index 123ecf7dd1..c35107d583 100644 --- a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php @@ -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]; } /** diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php index 74235c46c1..52c4a9b5c4 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php @@ -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 ])); }