Skip to content

Commit

Permalink
getRow now accepts negative offsets (although performance won't be as…
Browse files Browse the repository at this point in the history
… good when using negative offsets since the entire set has to be pulled)
  • Loading branch information
nozavroni committed Jul 25, 2018
1 parent faa3e90 commit da0bd3f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/CSVelte/Reader.php
Expand Up @@ -106,6 +106,9 @@ public function getDialect()
public function getRow($offset = null)
{
if (!is_null($offset)) {
if ($offset < 0) {
return collect($this->toArray())->getValueAt($offset);
}
foreach ($this as $key => $row) {
if ($key === $offset) {
return $row;
Expand Down
33 changes: 33 additions & 0 deletions tests/CSVelte/ReaderTest.php
Expand Up @@ -162,6 +162,39 @@ public function testGetRowUsingOffsetReturnsRowAtGivenOffset()
], $reader->getRow(25));
}

public function testGetRowUsingNegativeOffsetReturnsRowAtGivenOffset()
{
$source = to_stream(fopen($this->getFilePathFor('commaNewlineHeader'), 'r+'));
$reader = new Reader($source);
$this->assertSame([
"Bank Name" => "Vantage Point Bank",
"City" => "Horsham",
"ST" => "PA",
"CERT" => "58531",
"Acquiring Institution" => "First Choice Bank",
"Closing Date" => "28-Feb-14",
"Updated Date" => "3-Mar-15"
], $reader->getRow(-5));
$this->assertSame([
"Bank Name" => "Valley Bank",
"City" => "Moline",
"ST" => "IL",
"CERT" => "10450",
"Acquiring Institution" => "Great Southern Bank",
"Closing Date" => "20-Jun-14",
"Updated Date" => "26-Jun-15"
], $reader->getRow(-10));
$this->assertSame([
"Bank Name" => "The Bank of Georgia",
"City" => "Peachtree City",
"ST" => "GA",
"CERT" => "35259",
"Acquiring Institution" => "Fidelity Bank",
"Closing Date" => "2-Oct-15",
"Updated Date" => "13-Apr-16"
], $reader->getRow(-25));
}

public function testGetColumnReturnsColumn()
{
$source = to_stream(fopen($this->getFilePathFor('commaNewlineHeader'), 'r+'));
Expand Down

0 comments on commit da0bd3f

Please sign in to comment.