diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-12-11 09:13:56 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-12-11 09:13:56 +0100 |
commit | ffc2e213fab7032262130140c0d57c559380f383 (patch) | |
tree | 3f88ba0026806957e1adb24ebc97d023452ec310 | |
parent | fbe42a2629bb378f4d9841df4bf3de4ca33def53 (diff) | |
download | csv-ffc2e213fab7032262130140c0d57c559380f383.zip csv-ffc2e213fab7032262130140c0d57c559380f383.tar.gz csv-ffc2e213fab7032262130140c0d57c559380f383.tar.bz2 |
improving added newline feature
-rwxr-xr-x | examples/writing.php | 3 | ||||
-rw-r--r-- | src/AbstractCsv.php | 3 | ||||
-rw-r--r-- | src/Config/Controls.php | 33 | ||||
-rw-r--r-- | src/Writer.php | 47 | ||||
-rw-r--r-- | test/WriterTest.php | 6 |
5 files changed, 46 insertions, 46 deletions
diff --git a/examples/writing.php b/examples/writing.php index 557e74a..a625581 100755 --- a/examples/writing.php +++ b/examples/writing.php @@ -9,8 +9,9 @@ require '../vendor/autoload.php'; $writer = Writer::createFromFileObject(new SplTempFileObject()); //the CSV file will be created into a temporary File $writer->setDelimiter("\t"); //the delimiter will be the tab character -$writer->setLineEnding("\r\n"); //use windows line endings for compatibility with some csv libraries +$writer->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries $writer->setEncodingFrom("utf-8"); +$writer->setNullHandlingMode(Writer::NULL_AS_EMPTY); //if a null content cell is encountered it will be converted into a empty content $headers = ["position" , "team", "played", "goals difference", "points"]; $writer->insertOne($headers); diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 0986f18..8758caf 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.1.0 +* @version 6.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -218,7 +218,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate { $csv = new $class_name($this->path, $open_mode); $csv->delimiter = $this->delimiter; - $csv->lineEnding = $this->lineEnding; $csv->enclosure = $this->enclosure; $csv->escape = $this->escape; $csv->encodingFrom = $this->encodingFrom; diff --git a/src/Config/Controls.php b/src/Config/Controls.php index 7147d4d..e98058d 100644 --- a/src/Config/Controls.php +++ b/src/Config/Controls.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.1.0 +* @version 6.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -34,13 +34,6 @@ trait Controls protected $delimiter = ','; /** - * the line ending - * - * @var string - */ - protected $lineEnding = "\n"; - - /** * the field enclosure character (one character only) * * @var string @@ -98,30 +91,6 @@ trait Controls } /** - * set the line ending - * - * @param string $lineEnding - * - * @return $this - */ - public function setLineEnding($lineEnding = "\n") - { - $this->lineEnding = $lineEnding; - - return $this; - } - - /** - * return the current line ending - * - * @return string - */ - public function getLineEnding() - { - return $this->lineEnding; - } - - /** * detect the actual number of row according to a delimiter * * @param string $delimiter a CSV delimiter diff --git a/src/Writer.php b/src/Writer.php index 9049bba..420ff6f 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.1.0 +* @version 6.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -76,6 +76,13 @@ class Writer extends AbstractCsv protected $csv; /** + * newline character + * + * @var string + */ + protected $newline = "\n"; + + /** * Tell the class how to handle null value * * @param int $value a Writer null behavior constant @@ -105,6 +112,30 @@ class Writer extends AbstractCsv } /** + * set the line ending + * + * @param string $newline + * + * @return $this + */ + public function setNewline($newline) + { + $this->newline = (string) $newline; + + return $this; + } + + /** + * return the current line ending + * + * @return string + */ + public function getNewline() + { + return $this->newline; + } + + /** * Set Inserted row column count * * @param int $value @@ -193,11 +224,12 @@ class Writer extends AbstractCsv that requires '.$this->columns_count.' columns per row.' ); } - $this->getCsv()->fputcsv($data, $this->delimiter, $this->enclosure); + $csv = $this->getCsv(); + $csv->fputcsv($data, $this->delimiter, $this->enclosure); - if ($this->lineEnding !== "\n") { - $this->getCsv()->fseek(-1, \SEEK_CUR); - $this->getCsv()->fwrite($this->lineEnding); + if ("\n" !== $this->newline) { + $csv->fseek(-1, SEEK_CUR); + $csv->fwrite($this->newline); } return $this; @@ -295,10 +327,9 @@ class Writer extends AbstractCsv */ protected function getCsv() { - if (! is_null($this->csv)) { - return $this->csv; + if (is_null($this->csv)) { + $this->csv = $this->getIterator(); } - $this->csv = $this->getIterator(); return $this->csv; } diff --git a/test/WriterTest.php b/test/WriterTest.php index 2b43119..00f152c 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -217,11 +217,11 @@ class WriterTest extends PHPUnit_Framework_TestCase $this->assertSame(['john', 'doe', 'john.doe@example.com'], $reader->fetchOne(0)); } - - public function testCustomLineEnding() + public function testCustomNewline() { $csv = Writer::createFromFileObject(new SplTempFileObject()); - $csv->setLineEnding("\r\n"); + $this->assertSame("\n", $csv->getNewline()); + $csv->setNewline("\r\n"); $csv->insertOne(["jane", "doe"]); $this->assertSame("jane,doe\r\n", (string) $csv); |