Skip to content

Commit

Permalink
Merge pull request #10 from saloonphp/fix/deprecate-invalid-page-prop…
Browse files Browse the repository at this point in the history
…erty

Fix | v1 - Deprecate Invalid Page Property
  • Loading branch information
Sammyjo20 authored Dec 8, 2023
2 parents 9166ab1 + 740f38c commit e15977c
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ abstract class Paginator implements Iterator, Countable

/**
* Internal Marker For The Current Page
*
* @deprecated Use $currentPage instead
*/
protected int $page = 1;

/**
* Denotes the current page
*/
protected int $currentPage = 0;

/**
* When using async this is the total number of pages
*/
Expand Down Expand Up @@ -165,22 +172,23 @@ public function current(): Response|PromiseInterface
public function next(): void
{
$this->page++;
$this->currentPage++;
}

/**
* Get the key of the paginator
*/
public function key(): int
{
return $this->page - 1;
return $this->currentPage;
}

/**
* Check if the paginator has another page to retrieve
*/
public function valid(): bool
{
if (isset($this->maxPages) && $this->page > $this->maxPages) {
if (isset($this->maxPages) && ($this->currentPage + 1) > $this->maxPages) {
return false;
}

Expand All @@ -189,7 +197,7 @@ public function valid(): bool
}

if ($this->isAsyncPaginationEnabled()) {
return $this->page <= $this->totalPages ??= $this->getTotalPages($this->currentResponse);
return ($this->currentPage + 1) <= $this->totalPages ??= $this->getTotalPages($this->currentResponse);
}

return $this->isLastPage($this->currentResponse) === false;
Expand All @@ -201,6 +209,7 @@ public function valid(): bool
public function rewind(): void
{
$this->page = 1;
$this->currentPage = 0;
$this->currentResponse = null;
$this->totalResults = 0;
$this->totalPages = null;
Expand Down Expand Up @@ -305,12 +314,22 @@ public function getOriginalRequest(): Request

/**
* Get page
*
* @deprecated Use currentPage() instead
*/
public function getPage(): int
{
return $this->page;
}

/**
* Get the current page
*/
public function getCurrentPage(): int
{
return $this->currentPage;
}

/**
* Count the iterator
*/
Expand Down

0 comments on commit e15977c

Please sign in to comment.