for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types = 1);
namespace SimpleCrud\Queries\Traits;
use PDO;
trait HasPagination
{
private $page;
private $perPage = 10;
public function page(int $page): self
$this->page = $page;
$this->query->page($page);
query
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
return $this;
}
public function perPage(int $perPage = 10): self
$this->perPage = $perPage;
$this->query->perPage($perPage);
public function getPageInfo()
$query = clone $this->query;
$query->resetOrderBy();
$query->resetLimit();
$query->resetColumns();
$query->columns('COUNT(*)');
$statement = $query->perform();
$statement->setFetchMode(PDO::FETCH_NUM);
$page = $this->page;
$count = $statement->fetch();
$count = intval($count[0]);
return [
'totalRows' => $count,
'totalPages' => (int) ceil($count / $this->perPage),
'currentPage' => $count ? $page : null,
'previousPage' => $page > 1 ? $page - 1 : null,
'nextPage' => $count > ($page * $this->perPage) ? $page + 1 : null,
];
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: