diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 3118834d80175..131f872476ab1 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -11,6 +11,7 @@ CHANGELOG * Add `CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES` context option * Deprecate `AbstractNormalizerContextBuilder::withDefaultContructorArguments(?array $defaultContructorArguments)`, use `withDefaultConstructorArguments(?array $defaultConstructorArguments)` instead (note the missing `s` character in Contructor word in deprecated method) * Add `XmlEncoder::CDATA_WRAPPING_PATTERN` context option + * Add headers label support for `CsvEncoder` using `csv_headers` 7.0 --- diff --git a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php index a3112ae36226b..85a3a50ca7a93 100644 --- a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php @@ -86,11 +86,13 @@ public function encode(mixed $data, string $format, array $context = []): string } unset($value); + $labels = array_flip($headers); $headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers)); $endOfLine = $context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE]; if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) { - fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar); + $labels = array_map(fn ($header) => is_string($labels[$header] ?? null) ? $labels[$header] : $header, $headers); + fputcsv($handle, $labels, $delimiter, $enclosure, $escapeChar); if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) { fwrite($handle, $endOfLine); }