diff --git a/src/Csv.php b/src/Csv.php index 51ca6b0..5037791 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -2,6 +2,7 @@ namespace ParseCsv; +use ParseCsv\enums\FileProcessingModeEnum; use ParseCsv\extensions\DatatypeTrait; class Csv { @@ -379,16 +380,16 @@ class Csv { * @param string $file File location to save to * @param array $data 2D array of data * @param bool $append Append current data to end of target CSV, if file exists - * @param array $fields Field names + * @param array $fields Field names. Sets the header. If it is not set $this->titles would be used instead. * * @return bool */ - public function save($file = '', $data = array(), $append = false, $fields = array()) { + public function save($file = '', $data = array(), $append = FileProcessingModeEnum::MODE_FILE_OVERWRITE, $fields = array()) { if (empty($file)) { $file = &$this->file; } - $mode = $append ? 'ab' : 'wb'; + $mode = FileProcessingModeEnum::getAppendMode($append); $is_php = preg_match('/\.php$/i', $file) ? true : false; return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode); @@ -766,7 +767,7 @@ class Csv { * * @return string CSV data */ - public function unparse($data = array(), $fields = array(), $append = false, $is_php = false, $delimiter = null) { + public function unparse($data = array(), $fields = array(), $append = FileProcessingModeEnum::MODE_FILE_OVERWRITE, $is_php = false, $delimiter = null) { if (!is_array($data) || empty($data)) { $data = &$this->data; } diff --git a/src/enums/FileProcessingModeEnum.php b/src/enums/FileProcessingModeEnum.php new file mode 100644 index 0000000..ab88055 --- /dev/null +++ b/src/enums/FileProcessingModeEnum.php @@ -0,0 +1,28 @@ +saveAndCompare($expected); } + public function testSaveWithNewHeader() { + $this->csv->linefeed = "\n"; + $this->csv->titles = array("NewTitle"); + $expected = "NewTitle\n0444\n5555\n"; + $this->saveAndCompare($expected); + } + public function testSaveWithoutHeader() { $this->csv->linefeed = "\n"; $this->csv->heading = false;