From d12f72e2af4cd3b47e122e7bcb978cee87b194de Mon Sep 17 00:00:00 2001 From: turkishjoe Date: Wed, 1 May 2019 11:20:55 +0300 Subject: [PATCH] add trait for row to string converting without file --- src/CsvRowUtil.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/CsvWriter.php | 33 ++++++------------------ 2 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 src/CsvRowUtil.php diff --git a/src/CsvRowUtil.php b/src/CsvRowUtil.php new file mode 100644 index 0000000..9691c63 --- /dev/null +++ b/src/CsvRowUtil.php @@ -0,0 +1,63 @@ +getEnclosure() . + str_replace($this->getEnclosure(), str_repeat($this->getEnclosure(), 2), $column) . + $this->getEnclosure(); + } + return implode($this->getDelimiter(), $return) . $this->getLineBreak(); + } + + /** + * TODO: + * + * @return mixed + */ + abstract protected function getDelimiter(); + + /** + * TODO: + * + * @return mixed + */ + abstract protected function getLineBreak(); + + /** + * TODO: + * + * @return mixed + */ + abstract protected function getEnclosure(); + +} \ No newline at end of file diff --git a/src/CsvWriter.php b/src/CsvWriter.php index b5daea5..5596ca1 100644 --- a/src/CsvWriter.php +++ b/src/CsvWriter.php @@ -4,6 +4,8 @@ class CsvWriter extends AbstractCsvFile { + use CsvRowUtil; + /** * @var string */ @@ -97,33 +99,14 @@ public function writeRow(array $row) } } + /** - * @param array $row - * @return string - * @throws Exception + * TODO: + * + * @return mixed */ - public function rowToStr(array $row) + protected function getLineBreak() { - $return = []; - foreach ($row as $column) { - if (!( - is_scalar($column) - || is_null($column) - || ( - is_object($column) - && method_exists($column, '__toString') - ) - )) { - throw new Exception( - "Cannot write data into column: " . var_export($column, true), - Exception::WRITE_ERROR - ); - } - - $return[] = $this->getEnclosure() . - str_replace($this->getEnclosure(), str_repeat($this->getEnclosure(), 2), $column) . - $this->getEnclosure(); - } - return implode($this->getDelimiter(), $return) . $this->lineBreak; + return $this->lineBreak; } }