Skip to content

Commit

Permalink
Merge 6ed1e15 into 1fdeafa
Browse files Browse the repository at this point in the history
  • Loading branch information
scuben committed Sep 6, 2019
2 parents 1fdeafa + 6ed1e15 commit 2beb13a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Statement.php
Expand Up @@ -134,6 +134,18 @@ public function process(Reader $csv, array $header = []): ResultSet
return new ResultSet(new LimitIterator($iterator, $this->offset, $this->limit), $header);
}

public function processResultSet(ResultSet $resultSet, array $header = []): ResultSet
{
if ([] === $header) {
$header = $resultSet->getHeader();
}

$iterator = array_reduce($this->where, [$this, 'filter'], $resultSet->getRecords());
$iterator = $this->buildOrderBy($iterator);

return new ResultSet(new LimitIterator($iterator, $this->offset, $this->limit), $header);
}

/**
* Filters elements of an Iterator using a callback function.
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/ResultSetTest.php
Expand Up @@ -479,4 +479,34 @@ public function testJsonSerialize()
json_encode($result)
);
}

public function testResultSetProcess()
{
$data = [
['c1', 'c2', 'c3'],
['one', 'one', 'one'],
['two', 'two', 'two'],
['three', 'three', 'three'],
];

$tmp = new SplTempFileObject();
foreach ($data as $row) {
$tmp->fputcsv($row);
}

$reader = Reader::createFromFileObject($tmp)->setHeaderOffset(0);
$result = (new Statement())->where(static function (array $row) {
return $row['c1'] !== 'three';
})->process($reader);

self::assertEquals(2, $result->count());
self::assertSame(array_combine($data[0], $data[1]), $result->fetchOne());

$result = (new Statement())->where(static function (array $row) {
return $row['c1'] !== 'one';
})->processResultSet($result);

self::assertEquals(1, $result->count());
self::assertSame(array_combine($data[0], $data[2]), $result->fetchOne());
}
}

0 comments on commit 2beb13a

Please sign in to comment.