Skip to content

Commit 017afac

Browse files
committed
[Serializer] Remove deprecated escape_char functionality from CsvEncoder
1 parent 6ab4a14 commit 017afac

File tree

6 files changed

+43
-77
lines changed

6 files changed

+43
-77
lines changed

UPGRADE-8.0.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,41 @@ Security
333333
Serializer
334334
----------
335335

336+
* Remove escape character functionality from `CsvEncoder`
337+
338+
*Before*
339+
```php
340+
use Symfony\Component\Serializer\Encoder\CsvEncoder;
341+
342+
// Using escape character in encoding
343+
$encoder = new CsvEncoder();
344+
$csv = $encoder->encode($data, 'csv', [
345+
CsvEncoder::ESCAPE_CHAR_KEY => '\\',
346+
]);
347+
348+
// Using escape character with context builder
349+
use Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder;
350+
351+
$context = (new CsvEncoderContextBuilder())
352+
->withEscapeChar('\\')
353+
->toArray();
354+
```
355+
356+
*After*
357+
```php
358+
use Symfony\Component\Serializer\Encoder\CsvEncoder;
359+
360+
// The escape character functionality has been removed
361+
$encoder = new CsvEncoder();
362+
$csv = $encoder->encode($data, 'csv');
363+
364+
// The withEscapeChar() method no longer exists
365+
use Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder;
366+
367+
$context = (new CsvEncoderContextBuilder())
368+
->toArray();
369+
```
370+
336371
* Remove `AbstractNormalizerContextBuilder::withDefaultContructorArguments()`, use `withDefaultConstructorArguments()` instead
337372
* Change signature of `NameConverterInterface::normalize()` and `NameConverterInterface::denormalize()` methods:
338373

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
8.0
55
---
66

7+
* Remove `CsvEncoder::ESCAPE_CHAR_KEY` constant and escape character functionality
8+
* Remove `CsvEncoderContextBuilder::withEscapeChar()` method
79
* Remove `AbstractNormalizerContextBuilder::withDefaultContructorArguments()`, use `withDefaultConstructorArguments()` instead
810
* Change signature of `NameConverterInterface::normalize()` and `NameConverterInterface::denormalize()` methods:
911

src/Symfony/Component/Serializer/Context/Encoder/CsvEncoderContextBuilder.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,6 @@ public function withEnclosure(?string $enclosure): static
5757
return $this->with(CsvEncoder::ENCLOSURE_KEY, $enclosure);
5858
}
5959

60-
/**
61-
* Configures the escape character.
62-
*
63-
* Must be empty or a single character.
64-
*
65-
* @deprecated since Symfony 7.2, to be removed in 8.0
66-
*
67-
* @throws InvalidArgumentException
68-
*/
69-
public function withEscapeChar(?string $escapeChar): static
70-
{
71-
trigger_deprecation('symfony/serializer', '7.2', 'The "%s" method is deprecated. It will be removed in 8.0.', __METHOD__);
72-
73-
if (null !== $escapeChar && \strlen($escapeChar) > 1) {
74-
throw new InvalidArgumentException(\sprintf('The "%s" escape character must be empty or a single character.', $escapeChar));
75-
}
76-
77-
return $this->with(CsvEncoder::ESCAPE_CHAR_KEY, $escapeChar);
78-
}
7960

8061
/**
8162
* Configures the key separator when (un)flattening arrays.

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
2525
public const FORMAT = 'csv';
2626
public const DELIMITER_KEY = 'csv_delimiter';
2727
public const ENCLOSURE_KEY = 'csv_enclosure';
28-
/**
29-
* @deprecated since Symfony 7.2, to be removed in 8.0
30-
*/
31-
public const ESCAPE_CHAR_KEY = 'csv_escape_char';
3228
public const KEY_SEPARATOR_KEY = 'csv_key_separator';
3329
public const HEADERS_KEY = 'csv_headers';
3430
public const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas';
@@ -44,7 +40,6 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
4440
private array $defaultContext = [
4541
self::DELIMITER_KEY => ',',
4642
self::ENCLOSURE_KEY => '"',
47-
self::ESCAPE_CHAR_KEY => '',
4843
self::END_OF_LINE => "\n",
4944
self::ESCAPE_FORMULAS_KEY => false,
5045
self::HEADERS_KEY => [],
@@ -56,10 +51,6 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
5651

5752
public function __construct(array $defaultContext = [])
5853
{
59-
if (\array_key_exists(self::ESCAPE_CHAR_KEY, $defaultContext)) {
60-
trigger_deprecation('symfony/serializer', '7.2', 'Setting the "csv_escape_char" option is deprecated. The option will be removed in 8.0.');
61-
}
62-
6354
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
6455
}
6556

@@ -88,7 +79,7 @@ public function encode(mixed $data, string $format, array $context = []): string
8879
}
8980
}
9081

91-
[$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $outputBom] = $this->getCsvOptions($context);
82+
[$delimiter, $enclosure, $keySeparator, $headers, $escapeFormulas, $outputBom] = $this->getCsvOptions($context);
9283

9384
foreach ($data as &$value) {
9485
$flattened = [];
@@ -101,15 +92,15 @@ public function encode(mixed $data, string $format, array $context = []): string
10192
$endOfLine = $context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE];
10293

10394
if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
104-
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
95+
fputcsv($handle, $headers, $delimiter, $enclosure);
10596
if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
10697
fwrite($handle, $endOfLine);
10798
}
10899
}
109100

110101
$headers = array_fill_keys($headers, '');
111102
foreach ($data as $row) {
112-
fputcsv($handle, array_replace($headers, $row), $delimiter, $enclosure, $escapeChar);
103+
fputcsv($handle, array_replace($headers, $row), $delimiter, $enclosure);
113104
if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
114105
fwrite($handle, $endOfLine);
115106
}
@@ -150,9 +141,9 @@ public function decode(string $data, string $format, array $context = []): mixed
150141
$headerCount = [];
151142
$result = [];
152143

153-
[$delimiter, $enclosure, $escapeChar, $keySeparator, , , , $asCollection] = $this->getCsvOptions($context);
144+
[$delimiter, $enclosure, $keySeparator, , , , $asCollection] = $this->getCsvOptions($context);
154145

155-
while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
146+
while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure))) {
156147
$nbCols = \count($cols);
157148

158149
if (null === $headers) {
@@ -244,7 +235,6 @@ private function getCsvOptions(array $context): array
244235
{
245236
$delimiter = $context[self::DELIMITER_KEY] ?? $this->defaultContext[self::DELIMITER_KEY];
246237
$enclosure = $context[self::ENCLOSURE_KEY] ?? $this->defaultContext[self::ENCLOSURE_KEY];
247-
$escapeChar = $context[self::ESCAPE_CHAR_KEY] ?? $this->defaultContext[self::ESCAPE_CHAR_KEY];
248238
$keySeparator = $context[self::KEY_SEPARATOR_KEY] ?? $this->defaultContext[self::KEY_SEPARATOR_KEY];
249239
$headers = $context[self::HEADERS_KEY] ?? $this->defaultContext[self::HEADERS_KEY];
250240
$escapeFormulas = $context[self::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[self::ESCAPE_FORMULAS_KEY];
@@ -255,7 +245,7 @@ private function getCsvOptions(array $context): array
255245
throw new InvalidArgumentException(\sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, get_debug_type($headers)));
256246
}
257247

258-
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $outputBom, $asCollection];
248+
return [$delimiter, $enclosure, $keySeparator, $headers, $escapeFormulas, $outputBom, $asCollection];
259249
}
260250

261251
/**

src/Symfony/Component/Serializer/Tests/Context/Encoder/CsvEncoderContextBuilderTest.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,4 @@ public function testCannotSetMultipleBytesAsEnclosure()
122122
$this->contextBuilder->withEnclosure('');
123123
}
124124

125-
/**
126-
* @group legacy
127-
*/
128-
public function testCannotSetMultipleBytesAsEscapeChar()
129-
{
130-
$this->expectUserDeprecationMessage('Since symfony/serializer 7.2: The "Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder::withEscapeChar" method is deprecated. It will be removed in 8.0.');
131-
132-
$this->expectException(InvalidArgumentException::class);
133-
$this->contextBuilder->withEscapeChar('');
134-
}
135-
136-
/**
137-
* @group legacy
138-
*/
139-
public function testWithEscapeCharIsDeprecated()
140-
{
141-
$this->expectUserDeprecationMessage('Since symfony/serializer 7.2: The "Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder::withEscapeChar" method is deprecated. It will be removed in 8.0.');
142-
$context = $this->contextBuilder->withEscapeChar('\\');
143-
144-
$this->assertSame(['csv_escape_char' => '\\'], $context->toArray());
145-
}
146125
}

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -732,25 +732,4 @@ public static function provideIterable()
732732
yield 'generator' => [(fn (): \Generator => yield from $data)()];
733733
}
734734

735-
/**
736-
* @group legacy
737-
*/
738-
public function testPassingNonEmptyEscapeCharIsDeprecated()
739-
{
740-
$this->expectUserDeprecationMessage('Since symfony/serializer 7.2: Setting the "csv_escape_char" option is deprecated. The option will be removed in 8.0.');
741-
$encoder = new CsvEncoder(['csv_escape_char' => '@']);
742-
743-
$this->assertSame(
744-
[[
745-
'A, B@"' => 'D',
746-
'C' => 'E',
747-
]],
748-
$encoder->decode(<<<'CSV'
749-
"A, B@"", "C"
750-
"D", "E"
751-
CSV,
752-
'csv'
753-
)
754-
);
755-
}
756735
}

0 commit comments

Comments
 (0)