CsvFileDumper.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. * @return void
  36. */
  37. public function setCsvControl(string $delimiter = ';', string $enclosure = '"')
  38. {
  39. $this->delimiter = $delimiter;
  40. $this->enclosure = $enclosure;
  41. }
  42. protected function getExtension(): string
  43. {
  44. return 'csv';
  45. }
  46. }