Skip to content

Commit

Permalink
Merge pull request #21 from keboola/skip-lines-2
Browse files Browse the repository at this point in the history
feat: skip lines feature
  • Loading branch information
odinuv authored Feb 16, 2018
2 parents d7e2570 + 9650c3b commit 6f1d645
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php
before_script: composer install
php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"psr-0": {"Keboola\\Csv": "src/"}
},
"require-dev": {
"phpunit/phpunit": "~3.7"
"phpunit/phpunit": "^4.8"
}
}
23 changes: 21 additions & 2 deletions src/Keboola/Csv/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,42 @@ class CsvFile extends \SplFileInfo implements \Iterator
{
const DEFAULT_DELIMITER = ',';
const DEFAULT_ENCLOSURE = '"';
const DEFAULT_ESCAPED_BY = "";

protected $_delimiter;
protected $_enclosure;
protected $_escapedBy;
protected $_skipLines;

protected $_filePointer;
protected $_rowCounter = 0;
protected $_currentRow;
protected $_lineBreak;

public function __construct($fileName, $delimiter = self::DEFAULT_DELIMITER, $enclosure = self::DEFAULT_ENCLOSURE, $escapedBy = "")
public function __construct($fileName, $delimiter = self::DEFAULT_DELIMITER, $enclosure = self::DEFAULT_ENCLOSURE, $escapedBy = self::DEFAULT_ESCAPED_BY, $skipLines = 0)
{
parent::__construct($fileName);

$this->_escapedBy = $escapedBy;
$this->_setDelimiter($delimiter);
$this->_setEnclosure($enclosure);

$this->_setSkipLines($skipLines);
}

protected function _setSkipLines($skipLines)
{
$this->_validateSkipLines($skipLines);
$this->_skipLines = $skipLines;
return $this;
}

protected function _validateSkipLines($skipLines)
{
if (!is_int($skipLines) || $skipLines < 0) {
throw new InvalidArgumentException("Number of lines to skip must be a positive integer", Exception::INVALID_PARAM, null, 'invalidParam');
}
}

/**
* @param $delimiter
* @return CsvFile
Expand Down Expand Up @@ -259,6 +275,9 @@ public function valid()
public function rewind()
{
rewind($this->_getFilePointer());
for ($i = 0; $i < $this->_skipLines; $i++) {
$this->_readLine();
}
$this->_currentRow = $this->_readLine();
$this->_rowCounter = 0;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Keboola/Csv/CsvFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,47 @@ public function testNonStringWrite()

$csvFile->writeRow($row);
}

public function testSkipsHeaders()
{
$fileName = __DIR__ . '/_data/simple.csv';

$csvFile = new \Keboola\Csv\CsvFile($fileName, CsvFile::DEFAULT_DELIMITER, CsvFile::DEFAULT_ENCLOSURE, CsvFile::DEFAULT_ENCLOSURE, 1);
$this->assertEquals([
['15', '0'],
['18', '0'],
['19', '0'],
], iterator_to_array($csvFile));
}

public function testSkipNoLines()
{
$fileName = __DIR__ . '/_data/simple.csv';

$csvFile = new \Keboola\Csv\CsvFile($fileName, CsvFile::DEFAULT_DELIMITER, CsvFile::DEFAULT_ENCLOSURE, CsvFile::DEFAULT_ENCLOSURE, 0);
$this->assertEquals([
['id', 'isImported'],
['15', '0'],
['18', '0'],
['19', '0'],
], iterator_to_array($csvFile));
}

public function testSkipsMultipleLines()
{
$fileName = __DIR__ . '/_data/simple.csv';

$csvFile = new \Keboola\Csv\CsvFile($fileName, CsvFile::DEFAULT_DELIMITER, CsvFile::DEFAULT_ENCLOSURE, CsvFile::DEFAULT_ENCLOSURE, 3);
$this->assertEquals([
['19', '0'],
], iterator_to_array($csvFile));
}

public function testSkipsOverflow()
{
$fileName = __DIR__ . '/_data/simple.csv';

$csvFile = new \Keboola\Csv\CsvFile($fileName, CsvFile::DEFAULT_DELIMITER, CsvFile::DEFAULT_ENCLOSURE, CsvFile::DEFAULT_ENCLOSURE, 100);
$this->assertEquals([], iterator_to_array($csvFile));
}
}

0 comments on commit 6f1d645

Please sign in to comment.