|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
-
- namespace Lc\SovBundle\Generator;
-
- use Symfony\Component\HttpFoundation\StreamedResponse;
-
- class CsvGenerator
- {
- protected $arrayToExport = array();
- protected $columns;
- protected $titleDocument = 'csv_file';
-
- protected $convertEncoding = false ;
- protected $fromEncoding = 'UTF-8' ;
- protected $toEncoding = 'ISO-8859-1' ;
- protected $delimiter = ';' ;
-
- public function __construct()
- {
- }
-
- public function enableConvertEncoding($toEncoding, $fromEncoding = null)
- {
- $this->convertEncoding = true ;
- $this->toEncoding = $toEncoding ;
-
- if(!is_null($fromEncoding)) {
- $this->fromEncoding = $fromEncoding ;
- }
- }
-
- public function encode($value)
- {
- if($this->convertEncoding) {
- return mb_convert_encoding(
- $value,
- $this->toEncoding,
- $this->fromEncoding) ;
- }
-
- return $value ;
- }
-
- public function encodeArray($array)
- {
- if($array && is_array($array)) {
- return array_map(function($value) {
- return $this->encode($value) ;
- }, $array) ;
- }
-
- return $array ;
- }
-
- public function setTitle($title, $displayHeader = false){
-
- $this->titleDocument = $this->formatTitle($title);
-
- if($displayHeader) {
- array_unshift($this->arrayToExport, array(''));
- array_unshift($this->arrayToExport, array($title));
- }
- }
-
- public function setColumns($columns, $displayLegend = true) {
- $this->columns = array_fill_keys(array_keys($columns), null);
- if($displayLegend) $this->row($columns);
- }
-
- public function cell($column, $value){
- $this->arrayToExport[] = array_merge(
- $this->columns,
- array($column => $this->encode($value))
- );
- }
-
- public function row($values = null, $row = false){
-
- $values = $this->encodeArray($values) ;
-
- if(!$row){
- if($values)$this->arrayToExport[] = array_merge($this->columns, $values);
- else $this->arrayToExport[] = array();
- }else{
- if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values);
- else $this->arrayToExport[$row] = array();
- }
- }
-
- public function emptyRow()
- {
- $this->row([]) ;
- }
-
- public function dump(){
- dump($this->arrayToExport);
- }
-
- public function createCsv($path)
- {
- $handle = fopen($path, 'w+');
-
- foreach ($this->arrayToExport as $line) {
- fputcsv($handle, $line, $this->getDelimiter(), "\"");
- }
- fclose($handle);
- }
-
- public function getReponse(){
- $response = new StreamedResponse(function () {
- $this->createCsv('php://output');
- });
- $response->headers->set('Content-Encoding', $this->toEncoding);
- $response->headers->set('Content-Type', 'application/force-download');
- $response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
- return $response;
- }
- private function formatTitle($str) {
- $str = str_replace("-", ' ', $str);
- $str = preg_replace('/\s+/', '_',$str);;
- //$str = str_replace(" ", '_', $str);
- $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
-
- return $str;
- }
-
-
- public function getDelimiter()
- {
- return $this->delimiter ;
- }
- }
|