From 48a3cdbc5cfbf83fc71600101d5e607c20337cff Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Tue, 27 Feb 2018 14:18:00 +0100 Subject: [PATCH 1/2] new enum for file processing mode. extended documentation (comments #112) --- src/Csv.php | 9 +++++---- src/enums/FileProcessingModeEnum.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/enums/FileProcessingModeEnum.php diff --git a/src/Csv.php b/src/Csv.php index 74381e4..d9209a0 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -1,6 +1,7 @@ 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); @@ -760,7 +761,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 @@ + Date: Tue, 27 Feb 2018 14:33:26 +0100 Subject: [PATCH 2/2] new test for setting new headers before save (comments #82) --- tests/methods/SaveTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/methods/SaveTest.php b/tests/methods/SaveTest.php index 6e43b97..335844c 100644 --- a/tests/methods/SaveTest.php +++ b/tests/methods/SaveTest.php @@ -49,6 +49,13 @@ class SaveTest extends TestCase $this->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;