mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
new enum for file processing mode. extended documentation (comments #112)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ParseCsv;
|
namespace ParseCsv;
|
||||||
|
|
||||||
|
use ParseCsv\enums\FileProcessingModeEnum;
|
||||||
use ParseCsv\extensions\DatatypeTrait;
|
use ParseCsv\extensions\DatatypeTrait;
|
||||||
|
|
||||||
class Csv {
|
class Csv {
|
||||||
@@ -414,16 +415,16 @@ class Csv {
|
|||||||
* @param string $file File location to save to
|
* @param string $file File location to save to
|
||||||
* @param array $data 2D array of data
|
* @param array $data 2D array of data
|
||||||
* @param bool $append Append current data to end of target CSV, if file exists
|
* @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
|
* @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)) {
|
if (empty($file)) {
|
||||||
$file = &$this->file;
|
$file = &$this->file;
|
||||||
}
|
}
|
||||||
|
|
||||||
$mode = $append ? 'ab' : 'wb';
|
$mode = FileProcessingModeEnum::getAppendMode($append);
|
||||||
$is_php = preg_match('/\.php$/i', $file) ? true : false;
|
$is_php = preg_match('/\.php$/i', $file) ? true : false;
|
||||||
|
|
||||||
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
|
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
|
||||||
@@ -760,7 +761,7 @@ class Csv {
|
|||||||
*
|
*
|
||||||
* @return string CSV data
|
* @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)) {
|
if (!is_array($data) || empty($data)) {
|
||||||
$data = &$this->data;
|
$data = &$this->data;
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/enums/FileProcessingModeEnum.php
Normal file
28
src/enums/FileProcessingModeEnum.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ParseCsv\enums;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class FileProcessingEnum
|
||||||
|
*
|
||||||
|
* @package ParseCsv\enums
|
||||||
|
*
|
||||||
|
* todo extends a basic enum class after merging #121
|
||||||
|
*/
|
||||||
|
class FileProcessingModeEnum {
|
||||||
|
|
||||||
|
const __default = self::MODE_FILE_OVERWRITE;
|
||||||
|
|
||||||
|
const MODE_FILE_APPEND = true;
|
||||||
|
|
||||||
|
const MODE_FILE_OVERWRITE = false;
|
||||||
|
|
||||||
|
public static function getAppendMode($mode) {
|
||||||
|
if ($mode == self::MODE_FILE_APPEND){
|
||||||
|
return 'ab';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'wb';
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user