Skip to content

Commit

Permalink
Merge pull request #10 from korstiaan/lazy-load-headers
Browse files Browse the repository at this point in the history
Lazy load headers so we can still set delimiter / enclosure
  • Loading branch information
jwage committed Feb 17, 2013
2 parents 63db136 + 34012c2 commit 6d712cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
17 changes: 14 additions & 3 deletions lib/EasyCSV/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ class Reader extends AbstractBase
private $headersInFirstRow = true;
private $headers;
private $line;
private $init;

public function __construct($path, $mode = 'r+', $headersInFirstRow = true)
{
parent::__construct($path, $mode);
$this->headersInFirstRow = $headersInFirstRow;
$this->headers = $this->headersInFirstRow === true ? $this->getRow() : false;
$this->line = 0;
$this->line = 0;
}

public function getHeaders()
{
$this->init();
return $this->headers;
}

public function getRow()
{
$this->init();
if (($row = fgetcsv($this->handle, 1000, $this->delimiter, $this->enclosure)) !== false) {
$this->line++;
return $this->headers ? array_combine($this->headers, $row) : $row;
Expand All @@ -44,4 +46,13 @@ public function getLineNumber()
{
return $this->line;
}

protected function init()
{
if (true === $this->init) {
return;
}
$this->init = true;
$this->headers = $this->headersInFirstRow === true ? $this->getRow() : false;
}
}
40 changes: 27 additions & 13 deletions tests/EasyCSV/Tests/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,44 @@

namespace EasyCSV\Tests;

use EasyCSV\Reader;

class ReaderTest extends \PHPUnit_Framework_TestCase
{
private $reader;

public function setUp()
/**
* @dataProvider getReaders
*/
public function testOneAtAtime(Reader $reader)
{
$this->reader = new \EasyCSV\Reader(__DIR__ . '/read.csv');
}

public function testOneAtAtime()
{
while($row = $this->reader->getRow()) {
while($row = $reader->getRow()) {
$this->assertTrue(is_array($row));
$this->assertEquals(3, count($row));
}
}

public function testGetAll()
/**
* @dataProvider getReaders
*/
public function testGetAll(Reader $reader)
{
$this->assertEquals(5, count($this->reader->getAll()));
$this->assertEquals(5, count($reader->getAll()));
}

public function testGetHeaders()
/**
* @dataProvider getReaders
*/
public function testGetHeaders(Reader $reader)
{
$this->assertEquals(array("column1", "column2", "column3"), $reader->getHeaders());
}

public function getReaders()
{
$this->assertEquals(array("column1", "column2", "column3"), $this->reader->getHeaders());
$readerSemiColon = new \EasyCSV\Reader(__DIR__ . '/read_sc.csv');
$readerSemiColon->setDelimiter(';');
return array(
array(new \EasyCSV\Reader(__DIR__ . '/read.csv')),
array($readerSemiColon),
);
}
}
6 changes: 6 additions & 0 deletions tests/EasyCSV/Tests/read_sc.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"column1"; "column2"; "column3"
"1column2value"; "1column3value"; "1column4value"
"2column2value"; "2column3value"; "2column4value"
"3column2value"; "3column3value"; "3column4value"
"4column2value"; "4column3value"; "4column4value"
"5column2value"; "5column3value"; "5column4value"

0 comments on commit 6d712cf

Please sign in to comment.