Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan;
use Maslosoft\Mangan\Interfaces\Criteria\LimitableInterface;
use Maslosoft\Mangan\Interfaces\PaginationInterface;
/**
* Basic pagination class
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Pagination implements PaginationInterface
{
public $size = PaginationInterface::DefaultPageSize;
public $page = PaginationInterface::FirstPageId;
public $total = 0;
private $wasCount = false;
public function apply(LimitableInterface $criteria)
{
$limit = $criteria->getLimit();
if (empty($limit))
{
$criteria->setLimit($this->getLimit());
}
else
{
$this->setSize($limit);
}
/**
* TODO Pagination should revert to max or min page if value if out of range #78
* @see https://github.com/Maslosoft/Mangan/issues/78
*/
$criteria->setOffset($this->getOffset());
}
public function getPages()
{
return intval(ceil($this->total / $this->size));
}
public function setCount($total)
{
// Ensure positive total
$this->total = max($total, 0);
$this->wasCount = true;
// Recalculate max page, as order of setting
// count or page might be different
$this->page = max(min($this->getPages(), $this->page), 1);
return $this;
}
public function getSize()
{
return $this->size;
}
public function getPage()
{
return $this->page;
}
public function setSize($size)
{
$this->size = $size;
return $this;
}
public function setPage($page)
{
// See also setCount method
if ($this->wasCount)
{
$page = min($this->getPages(), $page);
}
$page = max($page, 1);
$this->page = $page;
return $this;
}
public function getLimit()
{
return $this->getSize();
}
public function getOffset()
{
// Pages are indexed from one, so substract 1 here
return ($this->getPage() - 1) * $this->getSize();
}
}
API documentation generated by ApiGen