summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-02-10 09:47:47 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-02-10 09:47:47 +0100
commitf4b09e18166c990879ef853c0f5aa44fe881ece3 (patch)
tree7c2c98afe87d68420df64e549851d5006a345a4b
parent6eafc3914df8ee3129cb1ef430351f8a9a3194c9 (diff)
downloadcsv-f4b09e18166c990879ef853c0f5aa44fe881ece3.zip
csv-f4b09e18166c990879ef853c0f5aa44fe881ece3.tar.gz
csv-f4b09e18166c990879ef853c0f5aa44fe881ece3.tar.bz2
improve SplFileObject flags handling
-rw-r--r--src/AbstractCsv.php5
-rw-r--r--src/Config/Controls.php26
-rw-r--r--test/ControlsTest.php15
3 files changed, 19 insertions, 27 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index 8fb4339..3365a08 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -104,6 +104,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
$this->path = $this->normalizePath($path);
$this->open_mode = strtolower($open_mode);
+ $this->flags = SplFileObject::READ_CSV|SplFileObject::DROP_NEW_LINE;
$this->initStreamFilter($this->path);
}
@@ -171,11 +172,13 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
protected function newInstance($class_name, $open_mode)
{
$csv = new $class_name($this->path, $open_mode);
- $csv->bom = $this->bom;
$csv->delimiter = $this->delimiter;
$csv->enclosure = $this->enclosure;
$csv->escape = $this->escape;
$csv->encodingFrom = $this->encodingFrom;
+ $csv->flags = $this->flags;
+ $csv->input_bom = $this->input_bom;
+ $csv->output_bom = $this->output_bom;
$csv->newline = $this->newline;
return $csv;
diff --git a/src/Config/Controls.php b/src/Config/Controls.php
index 5a9a0b3..952cb32 100644
--- a/src/Config/Controls.php
+++ b/src/Config/Controls.php
@@ -52,7 +52,7 @@ trait Controls
*
* @var int
*/
- protected $flags = SplFileObject::READ_CSV;
+ protected $flags;
/**
* newline character
@@ -236,30 +236,6 @@ trait Controls
return $this->flags;
}
- /**
- * Returns the BOM sequence of the given CSV
- *
- * @return string
- */
- public function getInputBOM()
- {
- $bom = [
- self::BOM_UTF8,
- self::BOM_UTF16_BE,
- self::BOM_UTF16_LE,
- self::BOM_UTF32_BE,
- self::BOM_UTF32_LE
- ];
- $csv = $this->getIterator();
- $csv->rewind();
- $line = $csv->fgets();
-
- $res = array_filter($bom, function ($sequence) use ($line) {
- return strpos($line, $sequence) === 0;
- });
-
- return array_shift($res);
- }
/**
* set the newline sequence characters
diff --git a/test/ControlsTest.php b/test/ControlsTest.php
index aaff9d2..0ed477c 100644
--- a/test/ControlsTest.php
+++ b/test/ControlsTest.php
@@ -83,10 +83,20 @@ class ControlsTest extends PHPUnit_Framework_TestCase
public function testGetBomOnInputWithBOM()
{
- $expected = "\x00\x00\xFE\xFFjohn,doe,john.doe@example.com".PHP_EOL
+ $expected = Reader::BOM_UTF32_BE."john,doe,john.doe@example.com".PHP_EOL
."jane,doe,jane.doe@example.com".PHP_EOL;
$reader = Reader::createFromString($expected);
$this->assertSame(Reader::BOM_UTF32_BE, $reader->getInputBOM());
+ $this->assertSame(Reader::BOM_UTF32_BE, $reader->getInputBOM());
+ }
+
+ public function testChangingBOMOnOutput()
+ {
+ $text = "john,doe,john.doe@example.com".PHP_EOL
+ ."jane,doe,jane.doe@example.com".PHP_EOL;
+ $reader = Reader::createFromString(Reader::BOM_UTF32_BE.$text);
+ $reader->setOutputBOM(Reader::BOM_UTF8);
+ $this->assertSame(Reader::BOM_UTF8.$text, $reader->__toString());
}
/**
@@ -163,9 +173,12 @@ class ControlsTest extends PHPUnit_Framework_TestCase
*/
public function testSetFlags()
{
+ $this->assertSame(SplFileObject::READ_CSV, $this->csv->getFlags() & SplFileObject::READ_CSV);
+ $this->assertSame(SplFileObject::DROP_NEW_LINE, $this->csv->getFlags() & SplFileObject::DROP_NEW_LINE);
$this->csv->setFlags(SplFileObject::SKIP_EMPTY);
$this->assertSame(SplFileObject::SKIP_EMPTY, $this->csv->getFlags() & SplFileObject::SKIP_EMPTY);
$this->assertSame(SplFileObject::READ_CSV, $this->csv->getFlags() & SplFileObject::READ_CSV);
+ $this->assertSame(SplFileObject::DROP_NEW_LINE, $this->csv->getFlags() & SplFileObject::DROP_NEW_LINE);
$this->csv->setFlags(-3);
}