Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2bf74ce

Browse filesBrowse files
minor #31684 [Serializer] Remove CsvEncoder "as_collection" deprecation & change default value (ogizanagi)
This PR was merged into the 5.0-dev branch. Discussion ---------- [Serializer] Remove CsvEncoder "as_collection" deprecation & change default value | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes (AppVeyor failure unrelated. See https://github.com/symfony/symfony/pull/31685/files) <!-- please add some, will be required by reviewers --> | Fixed tickets | #27715 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A <!-- required for new features --> As planned in #27715 Commits ------- 22dd071 [Serializer] Remove CsvEncoder "as_collection" deprecation & change default value
2 parents 7bfd937 + 22dd071 commit 2bf74ce
Copy full SHA for 2bf74ce

File tree

Expand file treeCollapse file tree

4 files changed

+9
-29
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+9
-29
lines changed

‎UPGRADE-5.0.md

Copy file name to clipboardExpand all lines: UPGRADE-5.0.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ SecurityBundle
382382
Serializer
383383
----------
384384

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

387388
Translation

‎src/Symfony/Component/Serializer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

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

1011
4.3.0
1112
-----

‎src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3333

3434
private $formulasStartCharacters = ['=', '-', '+', '@'];
3535
private $defaultContext = [
36+
self::AS_COLLECTION_KEY => true,
3637
self::DELIMITER_KEY => ',',
3738
self::ENCLOSURE_KEY => '"',
3839
self::ESCAPE_CHAR_KEY => '\\',
@@ -135,7 +136,7 @@ public function decode($data, $format, array $context = [])
135136
$headerCount = [];
136137
$result = [];
137138

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

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

186-
if ($context[self::AS_COLLECTION_KEY] ?? false) {
187+
if ($asCollection) {
187188
return $result;
188189
}
189190

190191
if (empty($result) || isset($result[1])) {
191192
return $result;
192193
}
193194

194-
if (!isset($context['as_collection'])) {
195-
@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);
196-
}
197-
198195
// If there is only one data line in the document, return it (the line), the result is not considered as a collection
199196
return $result[0];
200197
}
@@ -233,12 +230,13 @@ private function getCsvOptions(array $context): array
233230
$keySeparator = $context[self::KEY_SEPARATOR_KEY] ?? $this->defaultContext[self::KEY_SEPARATOR_KEY];
234231
$headers = $context[self::HEADERS_KEY] ?? $this->defaultContext[self::HEADERS_KEY];
235232
$escapeFormulas = $context[self::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[self::ESCAPE_FORMULAS_KEY];
233+
$asCollection = $context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY];
236234

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

241-
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas];
239+
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $asCollection];
242240
}
243241

244242
/**

‎src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php
+2-22Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,6 @@ public function testSupportsDecoding()
327327
$this->assertFalse($this->encoder->supportsDecoding('foo'));
328328
}
329329

330-
/**
331-
* @group legacy
332-
* @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.
333-
*/
334-
public function testDecodeLegacy()
335-
{
336-
$expected = ['foo' => 'a', 'bar' => 'b'];
337-
338-
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
339-
foo,bar
340-
a,b
341-
CSV
342-
, 'csv'));
343-
}
344-
345330
public function testDecodeAsSingle()
346331
{
347332
$expected = ['foo' => 'a', 'bar' => 'b'];
@@ -382,9 +367,7 @@ public function testDecode()
382367
a
383368

384369
CSV
385-
, 'csv', [
386-
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
387-
]));
370+
, 'csv'));
388371
}
389372

390373
public function testDecodeToManyRelation()
@@ -449,9 +432,7 @@ private function doTestDecodeCustomSettings(bool $legacy = false)
449432
a;bar-baz
450433
'hell''o';b;c
451434
CSV
452-
, 'csv', [
453-
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
454-
]));
435+
, 'csv'));
455436
}
456437

457438
public function testDecodeCustomSettingsPassedInContext()
@@ -466,7 +447,6 @@ public function testDecodeCustomSettingsPassedInContext()
466447
CsvEncoder::ENCLOSURE_KEY => "'",
467448
CsvEncoder::ESCAPE_CHAR_KEY => '|',
468449
CsvEncoder::KEY_SEPARATOR_KEY => '-',
469-
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
470450
]));
471451
}
472452

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.