Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\Translation\Dumper; |
| 13 | |
| 14 | use Symfony\Component\Translation\MessageCatalogue; |
| 15 | |
| 16 | /** |
| 17 | * CsvFileDumper generates a csv formatted string representation of a message catalogue. |
| 18 | * |
| 19 | * @author Stealth35 |
| 20 | */ |
| 21 | class CsvFileDumper extends FileDumper |
| 22 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 23 | private string $delimiter = ';'; |
| 24 | private string $enclosure = '"'; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * {@inheritdoc} |
| 28 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 29 | public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 30 | { |
| 31 | $handle = fopen('php://memory', 'r+'); |
| 32 | |
| 33 | foreach ($messages->all($domain) as $source => $target) { |
| 34 | fputcsv($handle, [$source, $target], $this->delimiter, $this->enclosure); |
| 35 | } |
| 36 | |
| 37 | rewind($handle); |
| 38 | $output = stream_get_contents($handle); |
| 39 | fclose($handle); |
| 40 | |
| 41 | return $output; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Sets the delimiter and escape character for CSV. |
| 46 | */ |
| 47 | public function setCsvControl(string $delimiter = ';', string $enclosure = '"') |
| 48 | { |
| 49 | $this->delimiter = $delimiter; |
| 50 | $this->enclosure = $enclosure; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * {@inheritdoc} |
| 55 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 56 | protected function getExtension(): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 57 | { |
| 58 | return 'csv'; |
| 59 | } |
| 60 | } |