/** * @param int $dataFormat * @param null $pdoArgs * @return object * @throws \Exception */ public function build($dataFormat = \PDO::FETCH_ASSOC, $pdoArgs = null) { $pager = array('pages' => array(), 'activePage' => $this->currentPage); //get data and total records from db $offset = ($this->currentPage - 1) * $this->pageSize; if ($this->rawQuery) { $query = "{$this->rawQuery} LIMIT {$offset}, {$this->pageSize}"; $this->rowCount = $this->db->execute()->rowCount(); $this->data = $this->query($query)->execute()->getResultSet($dataFormat, $pdoArgs); } else { $query = $this->db->getGeneratedQuery(false); $this->data = $this->db->limit($this->pageSize, $offset)->execute()->getResultSet($dataFormat, $pdoArgs); $this->rowCount = $this->db->query($query)->execute()->rowCount(); } //build paging data $last = $this->totalPages = ceil($this->rowCount / $this->pageSize); //last page $start = $this->currentPage - $this->range > 0 ? $this->currentPage - $this->range : 1; $end = $this->currentPage + $this->range < $last ? $this->currentPage + $this->range : $last; //go to first page if ($start > 1) { $pager['head'] = 1; } //pages in-between for ($page = $start; $page <= $end; $page++) { $pager['activePage'] = $this->currentPage == $page ? $page : $pager['activePage']; $pager['pages'][] = $page; } //go to last page if ($end < $last) { $pager['tail'] = $last; } //prev page if ($this->currentPage > 1) { $pager['previous'] = $this->currentPage - 1; } //next page if ($this->currentPage < $last) { $pager['next'] = $this->currentPage + 1; } $pager['data'] = $this->data; $pager['totalPages'] = $this->getTotalPages(); $pager['totalRecords'] = $this->getTotalRecords(); return (object) $pager; }