Skip to content

Commit

Permalink
Merge pull request #12 from douglas-88/dev
Browse files Browse the repository at this point in the history
Class Pagination
  • Loading branch information
douglas-88 authored Feb 6, 2020
2 parents 803b65b + 73cfec9 commit 30290b1
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index(){

$this->view("home",
[
"users" => $this->users->select()->get(),
"users" => $this->users->select()->paginate(2)->get(),
"title" => "Listando Usuários"
]
);
Expand Down
2 changes: 1 addition & 1 deletion app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class User extends Model
{
protected $table = "user";
protected $table = "users";
}
24 changes: 24 additions & 0 deletions app/traits/Links.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php


namespace App\traits;


trait Links
{
protected $maxLinks;

private function previous(){
if($this->page > 1){

}
}

private function next(){

}

public function links(){

}
}
26 changes: 23 additions & 3 deletions app/traits/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace App\traits;



use App\Model\User;
use Core\Paginate;


trait Read
{

use Links;
/**
* Método responsável por
* @param string $fields
Expand All @@ -30,6 +30,16 @@ public function bindAndExecute():\PDOStatement{
return $select;
}

/**
* Método responsável por Obter a quantidade de registros para a paginação.
* @return Int
*/
public function count():int {
$select = $this->connection->prepare($this->sql);
$select->execute($this->binds);
return $select->rowCount();
}

/**
* Método responsável por obter apenas o 1º Registro encontrado.
* @return object
Expand Down Expand Up @@ -83,4 +93,14 @@ public function where():User{

return $this;
}

public function paginate($perPage){

$this->paginate = new Paginate();
$this->paginate->getRegisters($this->count());
$this->paginate->paginate($perPage);
$this->sql .= $this->paginate->sqlPaginate();

return $this;
}
}
1 change: 1 addition & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Model extends Connection
protected $value;
protected $sql;
protected $binds;
protected $paginate;

use Create,Read,Update,Delete;

Expand Down
45 changes: 45 additions & 0 deletions src/Paginate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php


namespace Core;


class Paginate
{
private $page;
private $perPage;
private $offSet;
private $pages;
private $registers;

private function current(){
$this->page = filter_input(INPUT_GET,"page",FILTER_SANITIZE_NUMBER_INT) ?? 1;
}

private function perPage($perPage){
$this->perPage = $perPage ?? 30;
}

private function offSet(){
$this->offSet = ($this->page * $this->perPage) - $this->perPage;
}

private function pages(){
$this->pages = ceil($this->registers/$this->perPage);
}

public function getRegisters($registers){
$this->registers = $registers;
}

public function sqlPaginate(){
return " LIMIT {$this->perPage} OFFSET {$this->offSet} ";
}

public function paginate($perPage){
$this->current();
$this->perPage($perPage);
$this->offSet();
$this->pages();
}
}

0 comments on commit 30290b1

Please sign in to comment.