2026-03-30 09:10:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-09-11 16:55:12 +02:00
|
|
|
namespace Uwil\ArcadiaApiPhpClient\Lib;
|
2026-03-30 09:10:44 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Immutable value object representing a pagination range.
|
|
|
|
|
*/
|
|
|
|
|
final class Pagination
|
|
|
|
|
{
|
|
|
|
|
/** ERR_TOO_MANY_RECORDS fixed to 10k */
|
|
|
|
|
public const MAX_PAGE_SIZE = 10000;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly int $from,
|
|
|
|
|
private readonly int $to,
|
|
|
|
|
) {
|
|
|
|
|
if ($from < 0) {
|
|
|
|
|
throw new \InvalidArgumentException('From index must be non-negative');
|
|
|
|
|
}
|
|
|
|
|
if ($to < 0) {
|
|
|
|
|
throw new \InvalidArgumentException('To index must be non-negative');
|
|
|
|
|
}
|
|
|
|
|
if ($to <= $from) {
|
|
|
|
|
throw new \InvalidArgumentException('To index must be greater than from index');
|
|
|
|
|
}
|
|
|
|
|
if (($to - $from + 1) > self::MAX_PAGE_SIZE) {
|
|
|
|
|
throw new \InvalidArgumentException('Page size must be less than or equal to ' . self::MAX_PAGE_SIZE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function from(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->from;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function to(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->to;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pageSize(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->to - $this->from + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pageNumber(): int
|
|
|
|
|
{
|
|
|
|
|
return (int) ($this->from / $this->pageSize()) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function ofIndexes(int $from, int $to): self
|
|
|
|
|
{
|
|
|
|
|
return new self($from, $to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function ofPage(int $pageNumber, int $pageSize): self
|
|
|
|
|
{
|
|
|
|
|
if ($pageNumber <= 0) {
|
|
|
|
|
throw new \InvalidArgumentException('Page number must be positive and non-zero');
|
|
|
|
|
}
|
|
|
|
|
if ($pageSize <= 0) {
|
|
|
|
|
throw new \InvalidArgumentException('Page size must be positive and non-zero');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fromIndex = ($pageNumber - 1) * $pageSize;
|
|
|
|
|
$toIndex = max($pageNumber * $pageSize - 1, 1);
|
|
|
|
|
|
|
|
|
|
return new self($fromIndex, $toIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function ofOffsetAndLimit(int $offset, int $limit): self
|
|
|
|
|
{
|
|
|
|
|
if ($offset < 0) {
|
|
|
|
|
throw new \InvalidArgumentException('Offset must be non-negative');
|
|
|
|
|
}
|
|
|
|
|
if ($limit <= 0) {
|
|
|
|
|
throw new \InvalidArgumentException('Limit must be positive and non-zero');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fromIndex = $offset;
|
|
|
|
|
$toIndex = max($offset + $limit - 1, 1);
|
|
|
|
|
|
|
|
|
|
return new self($fromIndex, $toIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function firstPage(int $pageSize = 25): self
|
|
|
|
|
{
|
|
|
|
|
return self::ofPage(1, $pageSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __toString(): string
|
|
|
|
|
{
|
|
|
|
|
return sprintf('Pagination{from=%d, to=%d}', $this->from, $this->to);
|
|
|
|
|
}
|
|
|
|
|
}
|