菜谱项目

CsvFileDumper.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 = array())
  25. {
  26. $handle = fopen('php://memory', 'rb+');
  27. foreach ($messages->all($domain) as $source => $target) {
  28. fputcsv($handle, array($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. }