<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\PaginatorInterface;
use ArrayIterator;
class Paginator implements PaginatorInterface, \IteratorAggregate {
private null|ArrayIterator $itemIterator;
private int $totalItems;
private array $data;
private int $itemsPerPage;
public function __construct(array $data, $itemsPerPage, $totalItems, $page) {
$this->data = $data;
$this->itemsPerPage = $itemsPerPage;
$this->totalItems = $totalItems;
$this->page = $page;
}
public function getLastPage(): float {
return ceil($this->totalItems / $this->itemsPerPage);
}
public function getTotalItems(): float {
return $this->totalItems;
}
public function getCurrentPage(): float {
return $this->page;
}
public function getItemsPerPage(): float {
return $this->itemsPerPage;
}
public function count(): int {
return $this->totalItems;
}
public function getIterator(): \Traversable|array {
if ($this->itemIterator === null) {
$this->itemIterator = new \ArrayIterator(
$this->data
);
}
return $this->itemIterator;
}
}