Skip to content

Commit

Permalink
add LexerConfig setFlags/getFlags
Browse files Browse the repository at this point in the history
SplFileObject empty array support
  • Loading branch information
nagodon committed Dec 21, 2012
1 parent ce72870 commit 32fbd93
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion example/tsv-sample.php
Expand Up @@ -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
Expand All @@ -26,4 +27,4 @@
// parse
$lexer->parse('temperature.tsv', $interpreter);

var_dump($temperature);
var_dump($temperature);
3 changes: 2 additions & 1 deletion src/Goodby/CSV/Import/Standard/Lexer.php
Expand Up @@ -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;
Expand All @@ -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');
Expand Down
25 changes: 25 additions & 0 deletions src/Goodby/CSV/Import/Standard/LexerConfig.php
Expand Up @@ -32,6 +32,11 @@ class LexerConfig
*/
private $toCharset;

/**
* @var integer
*/
private $flags = \SplFileObject::READ_CSV;

/**
* Set delimiter
* @param string $delimiter
Expand Down Expand Up @@ -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;
}
}
9 changes: 9 additions & 0 deletions src/Goodby/CSV/Import/Tests/Standard/Unit/LexerConfigTest.php
Expand Up @@ -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());
}
}

0 comments on commit 32fbd93

Please sign in to comment.