2 namespace Utility\Export;
8 private $_delimiter = ',';
9 private $_enclosure = '"';
10 private $_escape = '\\';
11 private $_recordLength = 0;
13 const WRITE_OVERWRITE = 1;
14 const WRITE_APPEND = 2;
16 public function __construct($file, $writeMethod = self::WRITE_OVERWRITE)
19 $method = self::WRITE_OVERWRITE == $writeMethod
22 $this->_fileHandle = fopen($this->_file, $method);
23 if (!$this->_fileHandle)
25 throw new \Exception('File ' . $this->_file . ' could not be opened for writing');
29 public function setDelimiter($delimiter)
31 $this->_delimiter = $delimiter;
32 if (!empty($this->_headers))
35 $this->_headers = $this->getRecord(self::FETCH_LAZY);
39 public function setEnclosure($enclosure)
41 $this->_enclosure = $enclosure;
44 public function setEscape($escape)
46 $this->_escape = $escape;
49 public function setRecordLength($length)
51 $this->_recordLength = $length;
55 * Writes a record to the file
58 public function putRecord($record)
60 $bytesWritten = fputcsv($this->_fileHandle, $record, $this->_delimiter, $this->_enclosure);
61 return 0 < $bytesWritten
66 public function __destruct()
68 if ($this->_fileHandle)
70 fclose($this->_fileHandle);