CsvFileDumper.php 1.3 KB

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