* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_Renderer'); /** * Datasource renderer CSV * * @version 1.0.0 * @package WB * @subpackage db */ class WBDatasource_Renderer_CSV extends WBDatasource_Renderer { /** * Configuration parameters */ protected $config = array( 'addbom' => 0, 'delimiter' => ';', 'enclosure' => '"' ); /** * temporary file * @var WBFile */ protected $file; /** * curren file stream * @var resource */ protected $stream; /** * cleanup * * remove temporary file */ public function __destruct() { if ($this->file) { $this->file->unlink(); } } /** * start rendering * * Open stream to render view */ public function start($view) { $this->config['addbom'] = intval($this->config['addbom']); if ($this->file) { $this->file->unlink(); } $this->file = WBClass::create('WBFile'); $this->file->mkdir('var/tmp'); $this->file->tempnam('csv'); } /** * render single item * * The consumer for data source iteratrion * @param array $item */ public function renderItem($item) { // Init CSV file if (!$this->stream) { $this->stream = fopen($this->file->realpath(), 'w'); if ($this->config['addbom']) { fputs($this->stream, "\xEF\xBB\xBF"); } fputcsv($this->stream, array_keys($item), $this->config['delimiter'], $this->config['enclosure']); } fputcsv($this->stream, array_values($item), $this->config['delimiter'], $this->config['enclosure']); } /** * finish rendering * * Things to do after iterating through datasource */ public function end() { fclose($this->stream); $this->stream = null; } /** * get actual file * * @return WBFile */ public function getFile() { return $this->file; } } ?>