dev-think-one /
laravel-iprosoftware-sync
| 1 | <?php |
||
| 2 | |||
| 3 | namespace IproSync\Ipro; |
||
| 4 | |||
| 5 | class PullPagination |
||
| 6 | { |
||
| 7 | protected int $startPage = 1; |
||
| 8 | /** |
||
| 9 | * 0 = until end |
||
| 10 | * @var int |
||
| 11 | */ |
||
| 12 | protected int $endPage = 0; |
||
| 13 | protected int $perPage = 300; |
||
| 14 | |||
| 15 | public function __construct(int $startPage = 1, int $endPage = 0, int $perPage = 300) |
||
| 16 | { |
||
| 17 | $this->startPage = $startPage; |
||
| 18 | $this->endPage = $endPage; |
||
| 19 | $this->perPage = $perPage; |
||
| 20 | } |
||
| 21 | |||
| 22 | public static function page(int $page = 1, int $perPage = 300): static |
||
| 23 | { |
||
| 24 | return new static($page, $page, $perPage); |
||
| 25 | } |
||
| 26 | |||
| 27 | public static function pages(int $startPage = 1, int $endPage = 0, int $perPage = 300): static |
||
| 28 | { |
||
| 29 | return new static($startPage, $endPage, $perPage); |
||
| 30 | } |
||
| 31 | |||
| 32 | public static function allPages(int $perPage = 300): static |
||
| 33 | { |
||
| 34 | return new static(1, 0, $perPage); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function startPage(int $startPage = 1): static |
||
| 38 | { |
||
| 39 | $this->startPage = $startPage; |
||
| 40 | |||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function endPage(int $endPage = 0): static |
||
| 45 | { |
||
| 46 | $this->endPage = $endPage; |
||
| 47 | |||
| 48 | return $this; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function perPage(int $perPage = 300): static |
||
| 52 | { |
||
| 53 | $this->perPage = $perPage; |
||
| 54 | |||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function amendQuery(array $query = []): array |
||
| 59 | { |
||
| 60 | $query['size'] = $this->perPage; |
||
| 61 | $query['index'] = $this->startPage; |
||
| 62 | |||
| 63 | return $query; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function hasNext(int $total): bool |
||
| 67 | { |
||
| 68 | if ($this->endPage |
||
| 69 | && ($this->endPage) <= ($this->startPage) |
||
| 70 | ) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | return ($this->perPage * $this->startPage) < $total; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function nextPagination(int $total): ?static |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 78 | { |
||
| 79 | if ($this->hasNext($total)) { |
||
| 80 | return new static($this->startPage + 1, $this->endPage, $this->perPage); |
||
| 81 | } |
||
| 82 | |||
| 83 | return null; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getStartPage(): int |
||
| 87 | { |
||
| 88 | return $this->startPage; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function getEndPage(): int |
||
| 92 | { |
||
| 93 | return $this->endPage; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getPerPage(): int |
||
| 97 | { |
||
| 98 | return $this->perPage; |
||
| 99 | } |
||
| 100 | } |
||
| 101 |