Skip to content

Commit

Permalink
Make paginator able to convert to json
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Oct 14, 2023
1 parent c68ef53 commit ba9124e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Paginator/OffsetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Generator;
use InvalidArgumentException;
use JsonSerializable;
use Yiisoft\Data\Reader\CountableDataInterface;
use Yiisoft\Data\Reader\OffsetableDataInterface;
use Yiisoft\Data\Reader\ReadableDataInterface;
Expand Down Expand Up @@ -35,7 +36,7 @@
*
* @implements PaginatorInterface<TKey, TValue>
*/
final class OffsetPaginator implements PaginatorInterface
final class OffsetPaginator implements PaginatorInterface, JsonSerializable
{
/**
* @var int Current page number.
Expand Down Expand Up @@ -236,4 +237,15 @@ private function getInternalTotalPages(): int
{
return max(1, $this->getTotalPages());
}

public function jsonSerialize(): mixed
{
return [
'currentPage' => $this->currentPage,
'pageSize' => $this->pageSize,
'totalPages' => $this->getTotalPages(),
'totalItems' => $this->getTotalItems(),
'items' => $this->read(),
];
}
}

0 comments on commit ba9124e

Please sign in to comment.