CsvFileDumper.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 $delimiter = ';';
  20. private $enclosure = '"';
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
  25. {
  26. $handle = fopen('php://memory', 'r+');
  27. foreach ($messages->all($domain) as $source => $target) {
  28. fputcsv($handle, [$source, $target], $this->delimiter, $this->enclosure);
  29. }
  30. rewind($handle);
  31. $output = stream_get_contents($handle);
  32. fclose($handle);
  33. return $output;
  34. }
  35. /**
  36. * Sets the delimiter and escape character for CSV.
  37. */
  38. public function setCsvControl(string $delimiter = ';', string $enclosure = '"')
  39. {
  40. $this->delimiter = $delimiter;
  41. $this->enclosure = $enclosure;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function getExtension()
  47. {
  48. return 'csv';
  49. }
  50. }