From 3aed341f338db60572306984e1893008e35ed913 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 24 Sep 2024 14:35:35 +0300 Subject: [PATCH] Taken into account data reader limit in `OffsetPaginator::getTotalItems()` --- CHANGELOG.md | 4 ++-- src/Paginator/OffsetPaginator.php | 9 ++++++++- tests/Paginator/OffsetPaginatorTest.php | 10 ++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce7ab9f..7b81381 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,8 +27,8 @@ - New #176: Add `OrderHelper` (@vjik) - New #173, #184: Add `$caseSensitive` parameter to `Like` filter to control whether the search must be case-sensitive or not (@arogachev) -- Enh #187: Limit set in data reader is now taken into account by offset paginator. Keyset paginator throws an exception - in this case (@samdark) +- Enh #187, #196: Limit set in data reader is now taken into account by offset paginator. Keyset paginator throws + an exception in this case (@samdark, @vjik) - Chg #187: Add `FilterableDataInterface::getFilter()`, `LimitableDataInterface::getLimit()`, `OffsetableDataInterface::getOffset()` (@samdark) - Chg #187: `LimitableDataInterface::withLimit()` now accepts `null` to indicate "no limit". `0` is now a valid limit diff --git a/src/Paginator/OffsetPaginator.php b/src/Paginator/OffsetPaginator.php index da719c6..90ffb2c 100644 --- a/src/Paginator/OffsetPaginator.php +++ b/src/Paginator/OffsetPaginator.php @@ -199,7 +199,14 @@ public function getOffset(): int */ public function getTotalItems(): int { - return $this->dataReader->count(); + $count = $this->dataReader->count(); + + $dataReaderLimit = $this->dataReader->getLimit(); + if ($dataReaderLimit !== null && $count > $dataReaderLimit) { + return $dataReaderLimit; + } + + return $count; } /** diff --git a/tests/Paginator/OffsetPaginatorTest.php b/tests/Paginator/OffsetPaginatorTest.php index 2656fa5..d91f791 100644 --- a/tests/Paginator/OffsetPaginatorTest.php +++ b/tests/Paginator/OffsetPaginatorTest.php @@ -616,6 +616,15 @@ public function testWithNulledPageToken(): void $this->assertFalse($token->isPrevious); } + public function testLimitedDataReaderTotalItems(): void + { + $dataReader = (new IterableDataReader(self::DEFAULT_DATASET))->withLimit(3); + + $paginator = new OffsetPaginator($dataReader); + + $this->assertSame(3, $paginator->getTotalItems()); + } + public function testLimitedDataReaderReducedPage(): void { $dataReader = (new IterableDataReader(self::DEFAULT_DATASET))->withLimit(3); @@ -630,6 +639,7 @@ public function testLimitedDataReaderReducedPage(): void } $this->assertSame(1, $count); + $this->assertSame(3, $paginator->getTotalItems()); } public function testLimitedDataReaderEqualPage(): void