Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- New #232: Add `All` and `None` filters (@vjik)
- Chg #233: Remove nullable types from `withFilter()` and `getFilter()` methods of `FilterableDataInterface` (@vjik)
- Bug #234: Fix handling of `null` values in `IterableDataReader` (@vjik)
- New #236: Add `PaginatorInterface::getFilter()` method (@vjik)

## 1.0.1 January 25, 2023

Expand Down
9 changes: 7 additions & 2 deletions src/Paginator/KeysetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function read(): iterable
}

if ($this->token !== null) {
$dataReader = $dataReader->withFilter($this->getFilter($sort));
$dataReader = $dataReader->withFilter($this->createFilter($sort));
$this->hasPreviousPage = $this->previousPageExist($dataReader, $sort);
}

Expand Down Expand Up @@ -284,6 +284,11 @@ public function isFilterable(): bool
return true;
}

public function getFilter(): FilterInterface
{
return $this->dataReader->getFilter();
}

public function withFilter(FilterInterface $filter): static
{
$new = clone $this;
Expand Down Expand Up @@ -376,7 +381,7 @@ private function previousPageExist(ReadableDataInterface $dataReader, Sort $sort
return !empty($dataReader->withFilter($reverseFilter)->readOne());
}

private function getFilter(Sort $sort): FilterInterface
private function createFilter(Sort $sort): FilterInterface
{
/**
* @psalm-var PageToken $this->token The code calling this method must ensure that page token is not null.
Expand Down
11 changes: 10 additions & 1 deletion src/Paginator/OffsetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,19 @@ public function isFilterable(): bool
return $this->dataReader instanceof FilterableDataInterface;
}

public function getFilter(): FilterInterface
{
if (!$this->isFilterable()) {
throw new LogicException('Data reader doesn\'t support filtering.');
}

return $this->dataReader->getFilter();
}

public function withFilter(FilterInterface $filter): static
{
if (!$this->isFilterable()) {
throw new LogicException('Changing filtering is not supported.');
throw new LogicException('Data reader doesn\'t support filtering.');
}

$new = clone $this;
Expand Down
14 changes: 12 additions & 2 deletions src/Paginator/PaginatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,26 @@ public function withSort(?Sort $sort): static;
public function getSort(): ?Sort;

/**
* @return bool Whether changing filter via {@see withFilter()} is supported.
* @return bool Whether data reader does support filtering.
*/
public function isFilterable(): bool;

/**
* Returns data reader filter.
*
* @throws LogicException When data reader doesn't support filter.
*
* @return FilterInterface Data reader filter.
*/
public function getFilter(): FilterInterface;

/**
* Returns new instance with data reading criteria set.
*
* @param FilterInterface $filter Data reading criteria.
*
* @throws LogicException When changing filter isn't supported.
* @throws LogicException When data reader doesn't support filter.
*
* @return static New instance.
*/
public function withFilter(FilterInterface $filter): static;
Expand Down
15 changes: 12 additions & 3 deletions tests/Paginator/KeysetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,15 +822,15 @@ public function testGetPreviousPageExistForCoverage(): void
$this->assertTrue($this->invokeMethod($paginator, 'previousPageExist', [$dataReader, $sort]));
}

public function testGetFilterForCoverage(): void
public function testCreateFilterForCoverage(): void
{
$sort = Sort::only(['id'])->withOrderString('id');
$dataReader = (new IterableDataReader([]))->withSort($sort);
$paginator = (new KeysetPaginator($dataReader))->withToken(PageToken::next('1'));

$this->assertInstanceOf(
GreaterThan::class,
$this->invokeMethod($paginator, 'getFilter', [$sort]),
$this->invokeMethod($paginator, 'createFilter', [$sort]),
);

$sort = Sort::only(['id'])
Expand All @@ -841,7 +841,7 @@ public function testGetFilterForCoverage(): void

$this->assertInstanceOf(
LessThan::class,
$this->invokeMethod($paginator, 'getFilter', [$sort]),
$this->invokeMethod($paginator, 'createFilter', [$sort]),
);
}

Expand Down Expand Up @@ -1253,4 +1253,13 @@ public function testPreviousPageIterativeReading(): void
// Verify we got all the data
$this->assertSame($dataSet, $allData);
}

public function testGetFilter(): void
{
$filter = new All();
$dataReader = (new IterableDataReader([]))->withFilter($filter)->withSort(Sort::only(['id']));
$paginator = new KeysetPaginator($dataReader);

$this->assertSame($filter, $paginator->getFilter());
}
}
23 changes: 21 additions & 2 deletions tests/Paginator/OffsetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Data\Paginator\PageToken;
use Yiisoft\Data\Paginator\PaginatorInterface;
use Yiisoft\Data\Reader\CountableDataInterface;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\Filter\Equals;
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
use Yiisoft\Data\Reader\LimitableDataInterface;
Expand Down Expand Up @@ -587,8 +588,8 @@ public function testWithFilterNonFilterableData(): void
$paginator = new OffsetPaginator(new StubOffsetData());

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Changing filtering is not supported.');
$paginator->withFilter(new Equals('id', 2));
$this->expectExceptionMessage('Data reader doesn\'t support filtering.');
$paginator->withFilter(new All());
}

public function testWithNulledPageToken(): void
Expand Down Expand Up @@ -754,4 +755,22 @@ public function testPreviousPageIterativeReading(): void
// Verify we got all the data
$this->assertSame($dataSet, $allData);
}

public function testGetFilter(): void
{
$filter = new All();
$dataReader = (new IterableDataReader([]))->withFilter($filter);
$paginator = new OffsetPaginator($dataReader);

$this->assertSame($filter, $paginator->getFilter());
}

public function testGetFilterWithNonFilterableDataReader(): void
{
$paginator = new OffsetPaginator(new StubOffsetData());

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Data reader doesn\'t support filtering.');
$paginator->getFilter();
}
}
Loading