src/DataProvider/Paginator.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\DataProvider;
  3. use ApiPlatform\Core\DataProvider\PaginatorInterface;
  4. use ArrayIterator;
  5. class Paginator implements PaginatorInterface\IteratorAggregate {
  6.     private null|ArrayIterator $itemIterator;
  7.     private int $totalItems;
  8.     private array $data;
  9.     private int $itemsPerPage;
  10.     public function __construct(array $data$itemsPerPage$totalItems$page) {
  11.         $this->data $data;
  12.         $this->itemsPerPage $itemsPerPage;
  13.         $this->totalItems $totalItems;
  14.         $this->page $page;
  15.     }
  16.     public function getLastPage(): float {
  17.         return ceil($this->totalItems $this->itemsPerPage);
  18.     }
  19.     public function getTotalItems(): float {
  20.         return $this->totalItems;
  21.     }
  22.     public function getCurrentPage(): float {
  23.         return $this->page;
  24.     }
  25.     public function getItemsPerPage(): float {
  26.         return $this->itemsPerPage;
  27.     }
  28.     public function count(): int {
  29.         return $this->totalItems;
  30.     }
  31.     public function getIterator(): \Traversable|array {
  32.         if ($this->itemIterator === null) {
  33.             $this->itemIterator = new \ArrayIterator(
  34.                 $this->data
  35.             );
  36.         }
  37.         return $this->itemIterator;
  38.     }
  39. }