From b1fc0a29b76dca9e7841a4c270d7a1d3cd286c9f Mon Sep 17 00:00:00 2001 From: MacFJA Date: Sat, 15 Jan 2022 17:01:36 +0100 Subject: [PATCH] Fix PaginatedResponse index --- CHANGELOG.md | 2 ++ src/Redis/Response/CursorResponse.php | 4 ++++ src/Redis/Response/PaginatedResponse.php | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f83da11..bcfc70e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,11 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support from JSON document (require RediSearch 2.2) - (dev) GitHub Actions +- Protection against division by 0 ### Fixed - (dev) Code coverage with XDebug 3 - (dev) Defined list of allowed Composer plugins +- Fix `PaginatedResponse` index (now start at 0, and have a linear progression) ## [2.0.2] diff --git a/src/Redis/Response/CursorResponse.php b/src/Redis/Response/CursorResponse.php index b81f052..231a96f 100644 --- a/src/Redis/Response/CursorResponse.php +++ b/src/Redis/Response/CursorResponse.php @@ -106,6 +106,10 @@ public function getPageSize(): int public function getPageCount(): int { + if (0 === $this->size) { + return 0; + } + return (int) ceil($this->totalCount / $this->size); } diff --git a/src/Redis/Response/PaginatedResponse.php b/src/Redis/Response/PaginatedResponse.php index b3d38f0..6b8a893 100644 --- a/src/Redis/Response/PaginatedResponse.php +++ b/src/Redis/Response/PaginatedResponse.php @@ -96,6 +96,10 @@ public function getPageSize(): int public function getPageCount(): int { + if (0 === $this->getPageSize()) { + return 0; + } + return (int) ceil($this->totalCount / $this->getPageSize()); } @@ -105,7 +109,7 @@ public function key() return 0; } - return (int) ceil(($this->lastCommand->getOffset() ?? 0) / $this->getPageSize()) + 1; + return (int) floor(($this->lastCommand->getOffset() ?? 0) / $this->getPageSize()); } public function valid()