Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
paginator.php
1<?php
2
4
5use Traversable;
6
7class Paginator implements \IteratorAggregate
8{
9 private int $limit;
10 private int $pages;
11 private array $offsets = [];
12
13 public function __construct(int $total, int $limit)
14 {
15 $this->limit = $limit;
16 $this->pages = ceil($total / $limit);
17 $this->paginate();
18 }
19
23 public function getIterator(): Traversable
24 {
25 return new \ArrayIterator($this->offsets);
26 }
27
28 private function paginate(): void
29 {
30 for ($page = 1; $page <= $this->pages; $page++)
31 {
32 $this->offsets[] = ($page - 1) * $this->limit;
33 }
34 }
35}