98 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Pagination;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* a page is an element of a pagination.
*/
class Page implements PageInterface
{
/**
* the number of item per page.
*
*/
protected int $itemPerPage;
/**
* the number of the current page.
*
* @var int
*/
protected int $number;
/**
* The route for the current page.
*
* @var string
*/
protected string $route;
/**
* Parameters for the route to the current page.
*
* @var array
*/
protected array $routeParameters;
/**
* The number of items in the whole iteration.
*
* @var int
*/
protected int $totalItems;
/**
* @var UrlGeneratorInterface
*/
protected $urlGenerator;
public function __construct(
int $number,
int $itemPerPage,
UrlGeneratorInterface $urlGenerator,
string $route,
array $routeParameters,
int $totalItems
) {
$this->urlGenerator = $urlGenerator;
$this->number = $number;
$this->itemPerPage = $itemPerPage;
$this->route = $route;
$this->routeParameters = $routeParameters;
$this->totalItems = $totalItems;
}
public function generateUrl(): string
{
return $this->urlGenerator->generate($this->route, $this->routeParameters);
}
public function getFirstItemNumber(): int
{
return ($this->number - 1) * $this->itemPerPage;
}
public function getLastItemNumber(): int
{
$last = $this->number * $this->itemPerPage - 1;
return $last < $this->totalItems ? $last : $this->totalItems;
}
public function getNumber(): int
{
return $this->number;
}
}