Last active
March 12, 2020 02:04
-
-
Save celsowm/5de916d4cd9b77ba76692687aaf82150 to your computer and use it in GitHub Desktop.
paginacao_usando_template_method_pattern.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'conexao.php'; | |
abstract class Paginator { | |
private \PDO $pdo; | |
private int $pagina; | |
private int $limit; | |
public function __construct(\PDO $pdo, int $pagina = 1, $limit = 5) { | |
$this->pdo = $pdo; | |
$this->pagina = $pagina; | |
$this->limit = $limit; | |
} | |
public function getPagina(): int { | |
return $this->pagina; | |
} | |
public function getLimit(): int { | |
return $this->limit; | |
} | |
protected abstract function getPaginateQuery(string $query, string $order = 'id'): string; | |
public function getPaginateStatement(string $query): \PDOStatement { | |
$query = $this->getPaginateQuery($query); | |
$statement = $this->pdo->prepare($query); | |
$statement->bindValue(':offset', (int) $this->getOffSet(), \PDO::PARAM_INT); | |
$statement->bindValue(':limit', (int) $this->limit, \PDO::PARAM_INT); | |
return $statement; | |
} | |
public function getOffSet() { | |
return ($this->pagina - 1) * $this->limit; | |
} | |
public function getTotal(string $query) { | |
$query_total = $this->pdo->query("SELECT COUNT(*) FROM ($query) q"); | |
return (int) $query_total->fetchColumn(); | |
} | |
} | |
class MySQLPaginator extends Paginator { | |
protected function getPaginateQuery(string $query, string $order = 'id'): string { | |
return "$query ORDER BY $order LIMIT :limit OFFSET :offset"; | |
} | |
} | |
class AnsiPaginator extends Paginator { | |
protected function getPaginateQuery(string $query, string $order = 'id'): string { | |
return "$query ORDER BY $order OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY"; | |
} | |
} | |
class HtmlUtil { | |
private static function montaLinha(array $row, $tag = 'td') { | |
return "<tr>" . implode('', array_map(function($row) use ($tag) { | |
return "<$tag>" . $row . "</$tag>"; | |
}, $row)) . "</tr>"; | |
} | |
public static function getTable(\PDOStatement $statement, array $header = []) { | |
$statement->execute(); | |
$table = "<table border>"; | |
$table .= (!empty($header)) ? self::montaLinha($header, 'th') : ''; | |
while ($row = $statement->fetch()) { | |
$table .= self::montaLinha($row); | |
} | |
return "$table </table>"; | |
} | |
public static function getPaginationLinks(int $pagina, int $limit, int $total): string { | |
$links = (($pagina - 1) > 0) ? "<a href='?pagina=" . ($pagina - 1) . "'>Anterior</a>" : "Anterior"; | |
$links .= " "; | |
return $links.= (($pagina) * $limit < $total) ? "<a href='?pagina=" . ($pagina + 1) . "'>Próximo</a>" : "Próximo"; | |
} | |
} | |
$pagina = (isset($_REQUEST['pagina'])) ? $_REQUEST['pagina'] : 1; | |
$mySql = new \MySQLPaginator(Conexao::getInstance()->getPdo(), $pagina); | |
$query = "SELECT * FROM livro"; | |
$statement = $mySql->getPaginateStatement("SELECT livro.id, livro.titulo, livro.preco, livro.isbn FROM livro"); | |
echo HtmlUtil::getTable($statement, ['ID','Título','Preço', 'ISBN']); | |
echo HtmlUtil::getPaginationLinks($pagina, $mySql->getLimit(), $mySql->getTotal($query)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment