-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
- dropped SKIP_EMPTY and READ_AHEAD due to strange behavior when using c... #25
Changes from 1 commit
c46d2a2
ceb15ac
30ed96b
a45d571
83a552f
c90b7cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,41 +2,88 @@ | |
|
||
namespace EasyCSV; | ||
|
||
/** | ||
* Class Reader | ||
* | ||
* @package EasyCSV | ||
*/ | ||
class Reader extends AbstractBase | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
private $headersInFirstRow = true; | ||
private $headers; | ||
/** | ||
* @var bool | ||
*/ | ||
private $headers = false; | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space between this property and the doc block below. |
||
* @var int | ||
*/ | ||
private $line; | ||
|
||
/** | ||
* @var | ||
*/ | ||
private $init; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $headerLine = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space between this property and the doc block below. |
||
/** | ||
* @var bool | ||
*/ | ||
private $lastLine = false; | ||
|
||
/** | ||
* @param $path | ||
* @param string $mode | ||
* @param bool $headersInFirstRow | ||
*/ | ||
public function __construct($path, $mode = 'r+', $headersInFirstRow = true) | ||
{ | ||
parent::__construct($path, $mode); | ||
$this->headersInFirstRow = $headersInFirstRow; | ||
$this->line = 0; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getHeaders() | ||
{ | ||
$this->init(); | ||
return $this->headers; | ||
} | ||
|
||
|
||
/** | ||
* @return array|bool | ||
*/ | ||
public function getRow() | ||
{ | ||
$this->init(); | ||
if ($this->handle->eof()) { | ||
return false; | ||
} | ||
|
||
if (($row = $this->handle->fgetcsv($this->delimiter, $this->enclosure)) !== false && $row != null) { | ||
$row = $this->handle->fgetcsv($this->delimiter, $this->enclosure); | ||
$isEmpty = $this->rowIsEmpty( $row ); | ||
|
||
if ($row !== false && $row != null && $isEmpty === FALSE ) { | ||
$this->line++; | ||
return $this->headers ? array_combine($this->headers, $row) : $row; | ||
} elseif( $isEmpty ){ | ||
// empty row, transparently try the next row | ||
return $this->getRow(); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAll() | ||
{ | ||
$data = array(); | ||
|
@@ -46,22 +93,107 @@ public function getAll() | |
return $data; | ||
} | ||
|
||
/** | ||
* @return int zero-based index | ||
*/ | ||
public function getLineNumber() | ||
{ | ||
return $this->line; | ||
return $this->handle->key(); | ||
} | ||
|
||
/** | ||
* @return int zero-based index | ||
*/ | ||
public function getLastLineNumber() | ||
{ | ||
if( $this->lastLine !== FALSE ) { | ||
return $this->lastLine; | ||
} | ||
|
||
$this->handle->seek($this->handle->getSize()); | ||
$lastLine = $this->handle->key(); | ||
|
||
$this->handle->rewind(); | ||
|
||
return $this->lastLine = $lastLine; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getCurrentRow() | ||
{ | ||
return str_getcsv( $this->handle->current(), $this->delimiter, $this->enclosure ); | ||
} | ||
|
||
/** | ||
* @param $lineNumber zero-based index | ||
*/ | ||
public function advanceTo( $lineNumber ) | ||
{ | ||
if( $this->headerLine > $lineNumber){ | ||
throw new \LogicException("Line Number $lineNumber is before the header line that was set"); | ||
} elseif( $this->headerLine === $lineNumber ){ | ||
throw new \LogicException("Line Number $lineNumber is equal to the header line that was set"); | ||
} | ||
|
||
$this->line = $lineNumber; | ||
|
||
$this->handle->seek( $lineNumber ); | ||
} | ||
|
||
/** | ||
* @param $lineNumber zero-based index | ||
*/ | ||
public function setHeaderLine( $lineNumber ) | ||
{ | ||
if( $lineNumber !== 0 ) { | ||
$this->headersInFirstRow = false; | ||
} else { | ||
return false; | ||
} | ||
|
||
$this->headerLine = $lineNumber; | ||
|
||
// seek to line before headers | ||
$this->handle->seek( $lineNumber ); | ||
|
||
// get headers | ||
$this->headers = $this->getCurrentRow(); | ||
} | ||
|
||
protected function init() | ||
{ | ||
if (true === $this->init) { | ||
return; | ||
} | ||
$this->init = true; | ||
$this->headers = $this->headersInFirstRow === true ? $this->getRow() : false; | ||
$this->init = true; | ||
|
||
if( $this->headersInFirstRow === true) { | ||
$this->handle->rewind(); | ||
|
||
$this->headers = $this->getRow(); | ||
} | ||
} | ||
|
||
protected function incrementLine() | ||
/** | ||
* @param $row | ||
* @return bool | ||
*/ | ||
protected function rowIsEmpty( $row ) | ||
{ | ||
$this->line++; | ||
$emptyRow = ( $row === array( NULL ) ); | ||
$emptyRowWithDelimiters = ( array_filter( $row ) === array() ); | ||
$isEmpty = FALSE; | ||
|
||
if ( $emptyRow ) { | ||
$isEmpty = TRUE; | ||
return $isEmpty; | ||
} elseif ( $emptyRowWithDelimiters ) { | ||
$isEmpty = TRUE; | ||
return $isEmpty; | ||
} | ||
|
||
return $isEmpty; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,40 @@ | |
|
||
class ReaderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $headerValues = array( "column1", "column2", "column3" ); | ||
protected $expectedRows = array ( | ||
0 => | ||
array ( | ||
'column1' => '1column2value', | ||
'column2' => '1column3value', | ||
'column3' => '1column4value', | ||
), | ||
1 => | ||
array ( | ||
'column1' => '2column2value', | ||
'column2' => '2column3value', | ||
'column3' => '2column4value', | ||
), | ||
2 => | ||
array ( | ||
'column1' => '3column2value', | ||
'column2' => '3column3value', | ||
'column3' => '3column4value', | ||
), | ||
3 => | ||
array ( | ||
'column1' => '4column2value', | ||
'column2' => '4column3value', | ||
'column3' => '4column4value', | ||
), | ||
4 => | ||
array ( | ||
'column1' => '5column2value', | ||
'column2' => '5column3value', | ||
'column3' => '5column4value', | ||
), | ||
); | ||
|
||
/** | ||
* @dataProvider getReaders | ||
*/ | ||
|
@@ -30,15 +64,125 @@ public function testGetAll(Reader $reader) | |
*/ | ||
public function testGetHeaders(Reader $reader) | ||
{ | ||
$this->assertEquals(array("column1", "column2", "column3"), $reader->getHeaders()); | ||
$this->assertEquals( $this->headerValues, $reader->getHeaders()); | ||
} | ||
|
||
/** | ||
* @dataProvider getReaders | ||
*/ | ||
public function testAdvanceto(Reader $reader) | ||
{ | ||
$reader->advanceTo( 3 ); | ||
|
||
$this->assertEquals( 3, $reader->getLineNumber() ); | ||
|
||
$reader->advanceTo( 0 ); | ||
|
||
$row = array | ||
( | ||
'column1' => '1column2value', | ||
'column2' => '1column3value', | ||
'column3' => '1column4value', | ||
); | ||
|
||
$actualRow = $reader->getRow(); | ||
$this->assertEquals( $row, $actualRow ); | ||
|
||
$reader->advanceTo( 3 ); | ||
|
||
$row = array | ||
( | ||
'column1' => '4column2value', | ||
'column2' => '4column3value', | ||
'column3' => '4column4value', | ||
); | ||
|
||
$this->assertEquals( $row, $reader->getRow() ); | ||
} | ||
|
||
/** | ||
* @dataProvider getReadersNoHeadersFirstRow | ||
*/ | ||
public function testAdvanceToNoHeadersFirstRow(Reader $reader) | ||
{ | ||
$row = array ( | ||
0 => 'Some Meta Data', | ||
1 => '', | ||
2 => '', | ||
); | ||
|
||
$actualRow = $reader->getRow(); | ||
$this->assertEquals( $row, $actualRow ); | ||
|
||
// give it the ol' one-two-switcharoo | ||
$reader->advanceTo(3); | ||
$reader->getRow(); | ||
$reader->advanceTo(0); | ||
|
||
$this->assertEquals( $row, $reader->getRow() ); | ||
} | ||
|
||
/** | ||
* @dataProvider getReaders | ||
*/ | ||
public function testSetHeaderLine( Reader $reader ) | ||
{ | ||
$headers = $this->headerValues; | ||
|
||
$this->assertEquals( $headers, $reader->getHeaders() ); | ||
|
||
$reader->setHeaderLine(0); | ||
|
||
$this->assertEquals( $headers, $reader->getHeaders() ); | ||
} | ||
|
||
/** | ||
* @dataProvider getReadersNoHeadersFirstRow | ||
*/ | ||
public function testSetHeaderLineNoHeadersFirstRow( Reader $reader ){ | ||
// set headers | ||
$reader->setHeaderLine( 3 ); | ||
|
||
$this->assertEquals( $this->headerValues, $reader->getHeaders() ); | ||
|
||
$rows = $reader->getAll(); | ||
|
||
$this->assertCount(5, $rows); | ||
$this->assertEquals($this->expectedRows, $rows); | ||
} | ||
|
||
/** | ||
* @dataProvider getReaders | ||
*/ | ||
public function testGetLastLineNumber( Reader $reader ) | ||
{ | ||
$this->assertEquals( 5, $reader->getLastLineNumber() ); | ||
} | ||
|
||
/** | ||
* @dataProvider getReadersNoHeadersFirstRow | ||
*/ | ||
public function testGetLastLineNumberNoHeadersFirstRow( Reader $reader ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be good to remove these spaces around, you can also see https://github.com/fabpot/php-cs-fixer |
||
{ | ||
$this->assertEquals( 10, $reader->getLastLineNumber() ); | ||
} | ||
|
||
public function getReaders() | ||
{ | ||
$readerSemiColon = new \EasyCSV\Reader(__DIR__ . '/read_sc.csv'); | ||
$readerSemiColon = new Reader(__DIR__ . '/read_sc.csv'); | ||
$readerSemiColon->setDelimiter(';'); | ||
return array( | ||
array(new Reader(__DIR__ . '/read.csv')), | ||
array($readerSemiColon), | ||
); | ||
} | ||
|
||
public function getReadersNoHeadersFirstRow() | ||
{ | ||
$readerSemiColon = new Reader(__DIR__ . '/read_header_line_sc.csv', 'r+', FALSE ); | ||
$readerSemiColon->setDelimiter(';'); | ||
return array( | ||
array(new \EasyCSV\Reader(__DIR__ . '/read.csv')), | ||
array(new Reader(__DIR__ . '/read_header_line.csv', 'r+', FALSE )), | ||
array($readerSemiColon), | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"Some Meta Data",, | ||
"Field: Value",, | ||
,, | ||
"column1","column2","column3" | ||
"1column2value","1column3value","1column4value" | ||
"2column2value","2column3value","2column4value" | ||
"3column2value","3column3value","3column4value" | ||
|
||
,, | ||
"4column2value","4column3value","4column4value" | ||
"5column2value","5column3value","5column4value" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove this docblock as it does not add value