From ef32bb421d08eae54b67f0289805e03c37b95848 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Fri, 17 Nov 2023 13:31:58 +1000 Subject: [PATCH] Pagination/AbstractStrategy shouldGetPage($current_page) --- .../API/Traits/Utility/Pagination/AbstractStrategy.php | 2 +- .../API/Traits/Utility/Pagination/CbpStrategy.php | 2 +- .../API/Traits/Utility/Pagination/ObpStrategy.php | 4 ++-- .../Traits/Utility/Pagination/PaginationIterator.php | 10 +++++----- .../Traits/Utility/Pagination/SinglePageStrategy.php | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php index f0a954f3..5838505c 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -53,5 +53,5 @@ protected function pageSize() } abstract public function page($getPageFn); - abstract public function shouldGetPage($position); + abstract public function shouldGetPage($current_page); } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index f392fbe3..6a7874fe 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -29,7 +29,7 @@ public function page($getPageFn) return $this->latestResponse->{$this->resourcesKey}; } - public function shouldGetPage($position) { + public function shouldGetPage($current_page) { return !$this->started || $this->hasMore; } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php index c32b4cd5..966355b2 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -18,7 +18,7 @@ public function page($getPageFn) return $response->{$this->resourcesKey}; } - public function shouldGetPage($position) { - return $this->pageNumber == 0 || $position >= $this->pageNumber * $this->pageSize(); + public function shouldGetPage($current_page) { + return $this->pageNumber == 0 || count($current_page) == 0; } } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php index a0a61200..15e11e02 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php @@ -14,7 +14,7 @@ class PaginationIterator implements Iterator private $strategy; private $method; private $position = 0; - private $items = []; + private $page = []; /** * @param mixed using trait FindAll. The resources collection, Eg: `$client->tickets()` which uses FindAll @@ -56,8 +56,8 @@ public function valid() #[\ReturnTypeWillChange] public function current() { - if (isset($this->items[$this->position])) { - return $this->items[$this->position]; + if (isset($this->page[$this->position])) { + return $this->page[$this->position]; } else { return null; } @@ -74,14 +74,14 @@ public function latestResponse() } private function getPageIfNeeded() { - if (isset($this->items[$this->position]) || !$this->strategy->shouldGetPage($this->position)) { + if (isset($this->page[$this->position]) || !$this->strategy->shouldGetPage($this->page)) { return; } $getPageFn = function () { return $this->clientList->{$this->method}($this->strategy->params()); }; - $this->items = $this->strategy->page($getPageFn); + $this->page = $this->strategy->page($getPageFn); $this->position = 0; } } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php index 819fade4..6e1e9286 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php @@ -19,7 +19,7 @@ public function page($getPageFn) return $response->{$this->resourcesKey}; } - public function shouldGetPage($position) { + public function shouldGetPage($current_page) { return !$this->started; } }