Skip to content
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

Add DataReaderInterface::readOne() #40

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Reader/DataReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ interface DataReaderInterface
*/
public function withLimit(int $limit);
public function read(): iterable;
public function readOne();
}
9 changes: 8 additions & 1 deletion src/Reader/Iterable/IterableDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function read(): iterable

// skip offset items
if ($skipped < $this->offset) {
$skipped++;
++$skipped;
continue;
}

Expand All @@ -149,6 +149,13 @@ public function read(): iterable
return $data;
}

public function readOne()
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
{
return (static function (iterable $data): \Generator {
yield from $data;
})($this->withLimit(1)->read())->current();
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
}

public function withOffset(int $offset): self
{
$new = clone $this;
Expand Down
10 changes: 10 additions & 0 deletions tests/Paginator/KeysetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ public function read(): iterable
return [];
}

public function readOne()
{
return null;
}

public function withFilter(FilterInterface $filter)
{
// do nothing
Expand All @@ -504,6 +509,11 @@ public function read(): iterable
return [];
}

public function readOne()
{
return null;
}

public function withSort(?Sort $sorting)
{
// do nothing
Expand Down
8 changes: 8 additions & 0 deletions tests/Paginator/OffsetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function read(): iterable
{
return [];
}
public function readOne()
{
return null;
}
public function count(): int
{
return 0;
Expand Down Expand Up @@ -80,6 +84,10 @@ public function read(): iterable
{
return [];
}
public function readOne()
{
return null;
}
public function count(): int
{
return 0;
Expand Down
20 changes: 20 additions & 0 deletions tests/Reader/IterableDataReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ public function testCounting(): void
$this->assertCount(5, $reader);
}

public function testReadOne(): void
{
$data = $this->getDataSet();
$reader = new IterableDataReader($data);

$this->assertSame($data[0], $reader->readOne());
}

public function testReadOneWithSortingAndOffset(): void
{
$sorting = (new Sort(['id', 'name']))->withOrder(['name' => 'asc']);

$data = $this->getDataSet();
$reader = (new IterableDataReader($data))
->withSort($sorting)
->withOffset(2);

$this->assertSame($this->getDataSetSortedByName()[2], $reader->readOne());
}

public function testsWithFilterIsImmutable(): void
{
$reader = new IterableDataReader([]);
Expand Down