CsvFileDumper.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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, $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. * @param string $delimiter Delimiter character
  39. * @param string $enclosure Enclosure character
  40. */
  41. public function setCsvControl($delimiter = ';', $enclosure = '"')
  42. {
  43. $this->delimiter = $delimiter;
  44. $this->enclosure = $enclosure;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function getExtension()
  50. {
  51. return 'csv';
  52. }
  53. }