diff options
-rw-r--r-- | src/Config/Factory.php | 7 | ||||
-rw-r--r-- | src/Writer.php | 62 | ||||
-rw-r--r-- | test/WriterTest.php | 3 |
3 files changed, 32 insertions, 40 deletions
diff --git a/src/Config/Factory.php b/src/Config/Factory.php index 3e3d0fe..00f69b2 100644 --- a/src/Config/Factory.php +++ b/src/Config/Factory.php @@ -112,11 +112,6 @@ trait Factory $obj = new SplTempFileObject(); $obj->fwrite(rtrim($str).$newline); - $res = static::createFromFileObject($obj); - if ('League\Csv\Writer' == get_class($res)) { - $res->setNewline($newline); - } - - return $res; + return static::createFromFileObject($obj); } } diff --git a/src/Writer.php b/src/Writer.php index 69b1f95..70e063f 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -43,6 +43,11 @@ class Writer extends AbstractCsv const NULL_AS_EMPTY = 3; /** + * disable null handling + */ + const DISABLE_NULL_HANDLING = 4; + + /** * the object current null handling mode * * @var int @@ -83,14 +88,6 @@ class Writer extends AbstractCsv protected $newline = "\n"; /** - * should the insertOne method check for the cell content validation - * before insertion or not - * - * @var boolean - */ - protected $checkCellContent = true; - - /** * Tell the class how to handle null value * * @param int $value a Writer null behavior constant @@ -101,7 +98,12 @@ class Writer extends AbstractCsv */ public function setNullHandlingMode($value) { - if (!in_array($value, [self::NULL_AS_SKIP_CELL, self::NULL_AS_EXCEPTION, self::NULL_AS_EMPTY])) { + if (!in_array($value, [ + self::NULL_AS_SKIP_CELL, + self::NULL_AS_EXCEPTION, + self::NULL_AS_EMPTY, + self::DISABLE_NULL_HANDLING, + ])) { throw new OutOfBoundsException('invalid value for null handling'); } $this->null_handling_mode = $value; @@ -188,21 +190,6 @@ class Writer extends AbstractCsv } /** - * This method activate or deactive validation of cell content before insertions - * this will boost data insertion on a large CSV - * - * @param bool $status - * - * @return static - */ - public function useFormatValidation($status) - { - $this->checkCellContent = (bool) $status; - - return $this; - } - - /** * Add multiple lines to the CSV your are generating * * a simple helper/Wrapper method around insertOne @@ -270,10 +257,10 @@ class Writer extends AbstractCsv protected function validateRow($row) { if (! is_array($row)) { - $row = str_getcsv((string) $row, $this->delimiter, $this->enclosure, $this->escape); + return str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape); } - if ($this->checkCellContent) { + if (self::DISABLE_NULL_HANDLING != $this->null_handling_mode) { array_walk($row, function ($value) { if (! $this->isConvertibleContent($value)) { throw new InvalidArgumentException( @@ -311,15 +298,15 @@ class Writer extends AbstractCsv */ protected function sanitizeColumnsContent(array $row) { - if (self::NULL_AS_EXCEPTION == $this->null_handling_mode) { - return $row; - } elseif (self::NULL_AS_EMPTY == $this->null_handling_mode) { + if (self::NULL_AS_EMPTY == $this->null_handling_mode) { return str_replace(null, '', $row); + } elseif (self::NULL_AS_SKIP_CELL == $this->null_handling_mode) { + return array_filter($row, function ($value) { + return !is_null($value); + }); } - return array_filter($row, function ($value) { - return !is_null($value); - }); + return $row; } /** @@ -370,6 +357,17 @@ class Writer extends AbstractCsv } /** + * {@inheritdoc} + */ + public static function createFromString($str, $newline = PHP_EOL) + { + $obj = parent::createFromString($str, $newline); + $obj->setNewline($newline); + + return $obj; + } + + /** * The destructor */ public function __destruct() diff --git a/test/WriterTest.php b/test/WriterTest.php index 22f1137..06e2db7 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -118,8 +118,7 @@ class WriterTest extends PHPUnit_Framework_TestCase 'john,doe,john.doe@example.com', ['john', null, 'john.doe@example.com'], ]; - $this->csv->setNullHandlingMode(Writer::NULL_AS_EMPTY); - $this->csv->useFormatValidation(false); + $this->csv->setNullHandlingMode(Writer::DISABLE_NULL_HANDLING); $this->csv->insertAll($expected); $iterator = new LimitIterator($this->csv->getIterator(), 2, 1); |