diff --git a/example/tsv-sample.php b/example/tsv-sample.php index 75fea0c..dbabb1c 100644 --- a/example/tsv-sample.php +++ b/example/tsv-sample.php @@ -12,6 +12,7 @@ // set up lexer $config = new LexerConfig(); $config->setDelimiter("\t"); +$config->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV); $lexer = new Lexer($config); // set up interpreter @@ -26,4 +27,4 @@ // parse $lexer->parse('temperature.tsv', $interpreter); -var_dump($temperature); \ No newline at end of file +var_dump($temperature); diff --git a/src/Goodby/CSV/Import/Standard/Lexer.php b/src/Goodby/CSV/Import/Standard/Lexer.php index d417679..d54c677 100644 --- a/src/Goodby/CSV/Import/Standard/Lexer.php +++ b/src/Goodby/CSV/Import/Standard/Lexer.php @@ -38,6 +38,7 @@ public function parse($filename, InterpreterInterface $interpreter) $escape = $this->config->getEscape(); $fromCharset = $this->config->getFromCharset(); $toCharset = $this->config->getToCharset(); + $flags = $this->config->getFlags(); if ( $fromCharset === null ) { $url = $filename; @@ -47,7 +48,7 @@ public function parse($filename, InterpreterInterface $interpreter) $csv = new SplFileObject($url); $csv->setCsvControl($delimiter, $enclosure, $escape); - $csv->setFlags(SplFileObject::READ_CSV); + $csv->setFlags($flags); $originalLocale = setlocale(LC_ALL, '0'); // Backup current locale setlocale(LC_ALL, 'en_US.UTF-8'); diff --git a/src/Goodby/CSV/Import/Standard/LexerConfig.php b/src/Goodby/CSV/Import/Standard/LexerConfig.php index 1ab7f4a..15648ee 100644 --- a/src/Goodby/CSV/Import/Standard/LexerConfig.php +++ b/src/Goodby/CSV/Import/Standard/LexerConfig.php @@ -32,6 +32,11 @@ class LexerConfig */ private $toCharset; + /** + * @var integer + */ + private $flags = \SplFileObject::READ_CSV; + /** * Set delimiter * @param string $delimiter @@ -131,4 +136,24 @@ public function getToCharset() { return $this->toCharset; } + + /** + * Set flags + * @param integer $flags + * @return LexerConfig + */ + public function setFlags($flags) + { + $this->flags = $flags; + return $this; + } + + /** + * Return flags + * @return integer + */ + public function getFlags() + { + return $this->flags; + } } diff --git a/src/Goodby/CSV/Import/Tests/Standard/Unit/LexerConfigTest.php b/src/Goodby/CSV/Import/Tests/Standard/Unit/LexerConfigTest.php index f0f4a8e..ddfdf06 100644 --- a/src/Goodby/CSV/Import/Tests/Standard/Unit/LexerConfigTest.php +++ b/src/Goodby/CSV/Import/Tests/Standard/Unit/LexerConfigTest.php @@ -41,4 +41,13 @@ public function testToCharset() $this->assertSame(null, $config->getToCharset()); $this->assertSame('UTF-8', $config->setToCharset('UTF-8')->getToCharset()); } + + public function testFlags() + { + $config = new LexerConfig(); + $this->assertSame(\SplFileObject::READ_CSV, $config->getFlags()); + $config->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV); + $flags = (\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV); + $this->assertSame($flags, $config->getFlags()); + } }