You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CsvGenerator.php 4.3KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Lc\SovBundle\Generator;
  3. use Symfony\Component\HttpFoundation\StreamedResponse;
  4. class CsvGenerator
  5. {
  6. protected $arrayToExport;
  7. protected $columns;
  8. protected $titleDocument;
  9. protected $convertEncoding ;
  10. protected $fromEncoding ;
  11. protected $toEncoding ;
  12. protected $delimiter ;
  13. public function __construct()
  14. {
  15. $this->arrayToExport = array();
  16. $this->titleDocument = 'csv_file';
  17. $this->convertEncoding = false ;
  18. $this->fromEncoding = 'UTF-8' ;
  19. $this->toEncoding = 'ISO-8859-1' ;
  20. $this->delimiter = ';' ;
  21. }
  22. public function enableConvertEncoding($toEncoding, $fromEncoding = null)
  23. {
  24. $this->convertEncoding = true ;
  25. $this->toEncoding = $toEncoding ;
  26. if(!is_null($fromEncoding)) {
  27. $this->fromEncoding = $fromEncoding ;
  28. }
  29. }
  30. public function encode($value)
  31. {
  32. if($this->convertEncoding) {
  33. return mb_convert_encoding(
  34. $value,
  35. $this->toEncoding,
  36. $this->fromEncoding) ;
  37. }
  38. return $value ;
  39. }
  40. public function encodeArray($array)
  41. {
  42. if($array && is_array($array)) {
  43. return array_map(function($value) {
  44. return $this->encode($value) ;
  45. }, $array) ;
  46. }
  47. return $array ;
  48. }
  49. public function setTitle($title, $displayHeader = false){
  50. $this->titleDocument = $this->formatTitle($title);
  51. if($displayHeader) {
  52. array_unshift($this->arrayToExport, array(''));
  53. array_unshift($this->arrayToExport, array($title));
  54. }
  55. }
  56. public function setColumns($columns, $displayLegend = true) {
  57. $this->columns = array_fill_keys(array_keys($columns), null);
  58. if($displayLegend) $this->row($columns);
  59. }
  60. public function cell($column, $value){
  61. $this->arrayToExport[] = array_merge(
  62. $this->columns,
  63. array($column => $this->encode($value))
  64. );
  65. }
  66. public function row($values = null, $row = false){
  67. $values = $this->encodeArray($values) ;
  68. if(!$row){
  69. if($values)$this->arrayToExport[] = array_merge($this->columns, $values);
  70. else $this->arrayToExport[] = array();
  71. }else{
  72. if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values);
  73. else $this->arrayToExport[$row] = array();
  74. }
  75. }
  76. public function emptyRow()
  77. {
  78. $this->row([]) ;
  79. }
  80. public function dump(){
  81. dump($this->arrayToExport);
  82. }
  83. public function createCsv($path)
  84. {
  85. $handle = fopen($path, 'w+');
  86. foreach ($this->arrayToExport as $line) {
  87. fputcsv($handle, $line, $this->getDelimiter(), "\"");
  88. }
  89. fclose($handle);
  90. }
  91. public function getReponse(){
  92. $response = new StreamedResponse(function () {
  93. $this->createCsv('php://output');
  94. });
  95. $response->headers->set('Content-Encoding', $this->toEncoding);
  96. $response->headers->set('Content-Type', 'application/force-download');
  97. $response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
  98. return $response;
  99. }
  100. private function formatTitle($str) {
  101. $str = str_replace("-", ' ', $str);
  102. $str = preg_replace('/\s+/', '_',$str);;
  103. //$str = str_replace(" ", '_', $str);
  104. $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  105. return $str;
  106. }
  107. public function getDelimiter()
  108. {
  109. return $this->delimiter ;
  110. }
  111. }