Skip to content

Commit

Permalink
discovered SplFileObject::fgetcsv() bugs implemented a workaround in …
Browse files Browse the repository at this point in the history
…the meantime, removed superfluous $line, more unit tests, updated README with examples
  • Loading branch information
jorgecolonconsulting committed Jul 16, 2015
1 parent 94c3d2e commit 8d42155
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 57 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ php:
- 5.5
- 5.6
- hhvm
- 7.0

before_script: composer install

matrix:
allow_failures:
- php: hhvm
- php: 7.0
fast_finish: true
17 changes: 17 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ Or you can get everything all at once:
print_r($reader->getAll());
```

If you have a file with the header in a different line:

```php
// our headers aren't on the first line
$reader = new \EasyCSV\Reader('read.csv', 'r+', false);
// zero-based index, so this is line 4
$reader->setHeaderLine(3);
```

Advance to a different line:

```
$reader->advanceTo(6);
```

More in the Reader unit test.

## Writer

To write CSV files we need to instantiate the EasyCSV writer class:
Expand Down
2 changes: 1 addition & 1 deletion lib/EasyCSV/AbstractBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class AbstractBase

public function __construct($path, $mode = 'r+')
{
if ( ! file_exists($path)) {
if (! file_exists($path)) {
touch($path);
}
$this->handle = new \SplFileObject($path, $mode);
Expand Down
45 changes: 28 additions & 17 deletions lib/EasyCSV/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@ class Reader extends AbstractBase
*/
private $headers = false;

/**
* @var int
*/
private $line;

/**
* @var
*/
private $init;

/**
* @var bool
* @var bool|int
*/
private $headerLine = false;

/**
* @var bool
* @var bool|int
*/
private $lastLine = false;

Expand All @@ -43,7 +38,6 @@ public function __construct($path, $mode = 'r+', $headersInFirstRow = true)
{
parent::__construct($path, $mode);
$this->headersInFirstRow = $headersInFirstRow;
$this->line = 0;
}

/**
Expand All @@ -62,17 +56,19 @@ public function getHeaders()
public function getRow()
{
$this->init();
if ($this->handle->eof()) {
if ($this->isEof()) {
return false;
}

$row = $this->handle->fgetcsv($this->delimiter, $this->enclosure);
$row = $this->getCurrentRow();
$isEmpty = $this->rowIsEmpty($row);

if ($row !== false && $row != null && $isEmpty === false) {
$this->line++;
if ($this->isEof() === false) {
$this->handle->next();
}

return $this->headers ? array_combine($this->headers, $row) : $row;
if ($isEmpty === false) {
return ($this->headers && is_array($this->headers)) ? array_combine($this->headers, $row) : $row;
} elseif ($isEmpty) {
// empty row, transparently try the next row
return $this->getRow();
Expand All @@ -81,6 +77,14 @@ public function getRow()
}
}

/**
* @return bool
*/
public function isEof()
{
return $this->handle->eof();
}

/**
* @return array
*/
Expand Down Expand Up @@ -138,7 +142,13 @@ public function advanceTo($lineNumber)
throw new \LogicException("Line Number $lineNumber is equal to the header line that was set");
}

$this->line = $lineNumber;
if ($lineNumber > 0) {
$this->handle->seek($lineNumber - 1);
} // check the line before

if ($this->isEof()) {
throw new \LogicException("Line Number $lineNumber is past the end of the file");
}

$this->handle->seek($lineNumber);
}
Expand All @@ -156,11 +166,10 @@ public function setHeaderLine($lineNumber)

$this->headerLine = $lineNumber;

// seek to line before headers
$this->handle->seek($lineNumber);

// get headers
$this->headers = $this->getCurrentRow();
$this->headers = $this->getRow();
}

protected function init()
Expand All @@ -173,6 +182,8 @@ protected function init()
if ($this->headersInFirstRow === true) {
$this->handle->rewind();

$this->headerLine = 0;

$this->headers = $this->getRow();
}
}
Expand Down
Loading

0 comments on commit 8d42155

Please sign in to comment.