| Conditions | 5 |
| Paths | 4 |
| Total Lines | 21 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | public function getPagination(string $page, int $totalRows, int $rowsPerPage = Constant::POSTS_PER_PAGE): array |
||
| 22 | { |
||
| 23 | $page = strtolower($page); |
||
| 24 | if (!$this->startsWith($page, "page-")) { |
||
| 25 | throw new \Exception("Pagination Error", "404"); |
||
| 26 | } |
||
| 27 | $pageNo = $this->removeFromBeginning($page, "page-"); |
||
| 28 | if (!filter_var($pageNo, FILTER_VALIDATE_INT)) { |
||
| 29 | throw new \Exception("Invalid page number"); |
||
| 30 | } |
||
| 31 | $offset = ($pageNo - 1) * $rowsPerPage; |
||
| 32 | $totalPages = ceil($totalRows / $rowsPerPage); |
||
| 33 | |||
| 34 | if ($pageNo > $totalPages && $totalPages != 0) { |
||
| 35 | throw new \Error("Pagination Number not found", "404"); |
||
| 36 | } |
||
| 37 | |||
| 38 | return array( |
||
| 39 | "pageNo" => $pageNo, |
||
| 40 | "offset" => $offset, |
||
| 41 | "totalPages" => $totalPages |
||
| 42 | ); |
||
| 44 | } |