Posted By

madflow on 05/10/11


Tagged

php csv


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


CSV Writer


 / Published in: PHP
 

  1. <?php
  2. /**
  3.  * Helper for creating/writing CSV documents.
  4.  * @license Public Domain
  5.  */
  6. class CsvWriter
  7. {
  8. /**
  9.   * The data to be written
  10.   * @var array|null
  11.   */
  12. protected $data = array();
  13.  
  14. /**
  15.   * The entry delimiter
  16.   * @var string
  17.   */
  18. protected $delimit = ';';
  19.  
  20. /**
  21.   * The char enclosing an antry
  22.   * @var string
  23.   */
  24. protected $enclose = '"';
  25.  
  26. /**
  27.   * The char escaping an enclosing char
  28.   * @var string
  29.   */
  30. protected $escape = '"';
  31.  
  32. /**
  33.   * The line terminator. Defaults to PHP_EOL @link http://php.net/manual/en/reserved.constants.php
  34.   * @var string
  35.   */
  36. protected $terminate = PHP_EOL;
  37.  
  38. /**
  39.   * The header
  40.   * @var array|null
  41.   */
  42. protected $header = null;
  43.  
  44. /**
  45.   * Constructor with optional parameters
  46.   * @param array $data The data
  47.   * @param array $params The parameter array. Allowed keys are: header,delimit,enclose,escape,terminate
  48.   */
  49. public function __construct(array $data = null, array $params = null)
  50. {
  51. $this->data = $data;
  52. if(null !== $params)
  53. $this->setParams($params);
  54. }
  55.  
  56. /**
  57.   * Set optional parameters. If there is a setter method for an array key - the method will be called
  58.   * @param array $params
  59.   */
  60. protected function setParams(array $params)
  61. {
  62. foreach($params as $key => $param)
  63. if(method_exists($this, 'setParam'. ucfirst($key)))
  64. call_user_func(array($this,'setParam'.ucfirst($key)), $param);
  65. }
  66.  
  67. /**
  68.   * Getter
  69.   * @return array
  70.   */
  71. public function getData()
  72. {
  73. return $this->data;
  74. }
  75.  
  76. /**
  77.   * Setter
  78.   * @param array $data
  79.   */
  80. public function setData(array $data)
  81. {
  82. $this->data = $data;
  83. }
  84.  
  85. /**
  86.   * Getter
  87.   * @return array|null
  88.   */
  89. public function getParamHeader()
  90. {
  91. return $this->header;
  92. }
  93.  
  94. /**
  95.   * Setter
  96.   * @param array $header
  97.   */
  98. public function setParamHeader(array $header)
  99. {
  100. $this->header = $header;
  101. }
  102.  
  103. /**
  104.   * Getter
  105.   * @return string
  106.   */
  107. public function getParamDelimit()
  108. {
  109. return $this->delimit;
  110. }
  111.  
  112. /**
  113.   * Setter
  114.   * @param string $delimit
  115.   */
  116. public function setParamDelimit($delimit)
  117. {
  118. $this->delimit = $delimit;
  119. }
  120.  
  121. /**
  122.   * Getter
  123.   * @return string
  124.   */
  125. public function getParamEnclose()
  126. {
  127. return $this->enclose;
  128. }
  129.  
  130. /**
  131.   * Setter
  132.   * @return string
  133.   */
  134. public function setParamEnclose($enclose)
  135. {
  136. $this->enclose = $enclose;
  137. }
  138.  
  139. /**
  140.   * Getter
  141.   * @return string
  142.   */
  143. public function getParamEscape()
  144. {
  145. return $this->escape;
  146. }
  147.  
  148. /**
  149.   * Setter
  150.   * @return string
  151.   */
  152. public function setParamEscape($escape)
  153. {
  154. $this->escape = $escape;
  155. }
  156.  
  157. /**
  158.   * Getter
  159.   * @return string
  160.   */
  161. public function getParamTerminate()
  162. {
  163. return $this->terminate;
  164. }
  165.  
  166. /**
  167.   * Setter
  168.   * @return string
  169.   */
  170. public function setParamTerminate($terminate)
  171. {
  172. $this->terminate = $terminate;
  173. }
  174.  
  175. /**
  176.   * Formats a row
  177.   * @param array $data
  178.   * @return string
  179.   */
  180. protected function formatRow(array $data)
  181. {
  182. $buffer = array();
  183. foreach((array)$data as $key => $item)
  184. {
  185. $buffer[] = $this->getParamEnclose()
  186. .str_replace($this->getParamEnclose(), $this->getParamEscape().$this->getParamEnclose(), $item)
  187. .$this->getParamEnclose();
  188. }
  189. return implode($this->getParamDelimit(), $buffer);
  190. }
  191.  
  192. /**
  193.   * Outputs the complete csv string.
  194.   * @return string
  195.   */
  196. protected function toString()
  197. {
  198. $buffer = array();
  199. if(null !== $this->getParamHeader())
  200. $buffer[] = $this->formatRow($this->getParamHeader());
  201. foreach((array)$this->getData() as $row)
  202. $buffer[] = $this->formatRow($row);
  203.  
  204. return implode($this->getParamTerminate(), $buffer);
  205. }
  206.  
  207. /**
  208.   * Save/Output the csv string
  209.   * If there is no parameter - the string will be returned
  210.   * If there is a parameter - the string will be saved
  211.   * @param string $fileName
  212.   * @return string|void
  213.   */
  214. public function save($fileName = null)
  215. {
  216. if(null === $fileName)
  217. {
  218. return $this->toString();
  219. }
  220. else
  221. {
  222. file_put_contents($fileName, $this->toString());
  223. }
  224. }
  225. }

Report this snippet  

You need to login to post a comment.