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; } }