AzuraCast/src/Paginator.php

250 lines
6.6 KiB
PHP
Raw Permalink Normal View History

<?php
2021-07-19 07:53:45 +02:00
declare(strict_types=1);
namespace App;
use App\Exception\InvalidRequestAttribute;
use App\Http\Response;
use App\Http\RouterInterface;
use App\Http\ServerRequest;
2021-06-10 05:22:13 +02:00
use Countable;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
2022-11-05 01:20:42 +01:00
use Generator;
2021-06-10 05:22:13 +02:00
use IteratorAggregate;
use Pagerfanta\Adapter\AdapterInterface;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Doctrine\Collections\CollectionAdapter;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Psr\Http\Message\ResponseInterface;
2022-05-31 13:41:35 +02:00
/**
* @template TKey of array-key
* @template T of mixed
* @implements IteratorAggregate<TKey, T>
*/
2022-07-01 09:41:04 +02:00
final class Paginator implements IteratorAggregate, Countable
{
2022-07-01 09:41:04 +02:00
private RouterInterface $router;
Dependency Update Changelogs summary: - pyrech/composer-changelogs updated from v1.8.0 to v1.8.1 patch See changes: https://github.com/pyrech/composer-changelogs/compare/v1.8.0...v1.8.1 Release notes: https://github.com/pyrech/composer-changelogs/releases/tag/v1.8.1 - james-heinrich/getid3 updated from dev-master@a440175 to dev-master@1e18087 See changes: https://github.com/JamesHeinrich/getID3/compare/a440175...1e18087 - doctrine/inflector updated from 2.0.3 to 2.0.4 patch See changes: https://github.com/doctrine/inflector/compare/2.0.3...2.0.4 Release notes: https://github.com/doctrine/inflector/releases/tag/2.0.4 - doctrine/common updated from 3.1.2 to 3.2.0 minor See changes: https://github.com/doctrine/common/compare/3.1.2...3.2.0 Release notes: https://github.com/doctrine/common/releases/tag/3.2.0 - doctrine/orm updated from 2.10.1 to 2.10.2 patch See changes: https://github.com/doctrine/orm/compare/2.10.1...2.10.2 Release notes: https://github.com/doctrine/orm/releases/tag/2.10.2 - behat/gherkin updated from v4.8.0 to v4.9.0 minor See changes: https://github.com/Behat/Gherkin/compare/v4.8.0...v4.9.0 Release notes: https://github.com/Behat/Gherkin/releases/tag/v4.9.0 - phpdocumentor/reflection-docblock updated from 5.2.2 to 5.3.0 minor See changes: https://github.com/phpDocumentor/ReflectionDocBlock/compare/5.2.2...5.3.0 Release notes: https://github.com/phpDocumentor/ReflectionDocBlock/releases/tag/5.3.0 - guzzlehttp/psr7 updated from 1.8.3 to 2.1.0 major See changes: https://github.com/guzzle/psr7/compare/1.8.3...2.1.0 Release notes: https://github.com/guzzle/psr7/releases/tag/2.1.0 - guzzlehttp/promises updated from 1.5.0 to 1.5.1 patch See changes: https://github.com/guzzle/promises/compare/1.5.0...1.5.1 Release notes: https://github.com/guzzle/promises/releases/tag/1.5.1 - guzzlehttp/guzzle updated from 7.3.0 to 7.4.0 minor See changes: https://github.com/guzzle/guzzle/compare/7.3.0...7.4.0 Release notes: https://github.com/guzzle/guzzle/releases/tag/7.4.0 - doctrine/migrations updated from 3.2.1 to 3.3.0 minor See changes: https://github.com/doctrine/migrations/compare/3.2.1...3.3.0 Release notes: https://github.com/doctrine/migrations/releases/tag/3.3.0 - gettext/gettext updated from v4.8.5 to v4.8.6 patch See changes: https://github.com/php-gettext/Gettext/compare/v4.8.5...v4.8.6 Release notes: https://github.com/php-gettext/Gettext/releases/tag/v4.8.6 - graham-campbell/guzzle-factory updated from v5.0.0 to v5.0.2 patch See changes: https://github.com/GrahamCampbell/Guzzle-Factory/compare/v5.0.0...v5.0.2 Release notes: https://github.com/GrahamCampbell/Guzzle-Factory/releases/tag/v5.0.2 - aws/aws-sdk-php updated from 3.198.0 to 3.199.2 minor See changes: https://github.com/aws/aws-sdk-php/compare/3.198.0...3.199.2 Release notes: https://github.com/aws/aws-sdk-php/releases/tag/3.199.2 - pagerfanta/core updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/core/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/core/releases/tag/v3.3.1 - pagerfanta/doctrine-collections-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-collections-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-collections-adapter/releases/tag/v3.3.1 - pagerfanta/doctrine-orm-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-orm-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-orm-adapter/releases/tag/v3.3.1 - ramsey/collection updated from 1.2.1 to 1.2.2 patch See changes: https://github.com/ramsey/collection/compare/1.2.1...1.2.2 Release notes: https://github.com/ramsey/collection/releases/tag/1.2.2 - squizlabs/php_codesniffer updated from 3.6.0 to 3.6.1 patch See changes: https://github.com/squizlabs/PHP_CodeSniffer/compare/3.6.0...3.6.1 Release notes: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.6.1 - phpstan/phpdoc-parser updated from 0.5.6 to 1.2.0 major See changes: https://github.com/phpstan/phpdoc-parser/compare/0.5.6...1.2.0 Release notes: https://github.com/phpstan/phpdoc-parser/releases/tag/1.2.0 - slevomat/coding-standard updated from 7.0.15 to 7.0.16 patch See changes: https://github.com/slevomat/coding-standard/compare/7.0.15...7.0.16 Release notes: https://github.com/slevomat/coding-standard/releases/tag/7.0.16 - egulias/email-validator updated from 3.1.1 to 3.1.2 patch See changes: https://github.com/egulias/EmailValidator/compare/3.1.1...3.1.2 Release notes: https://github.com/egulias/EmailValidator/releases/tag/3.1.2 - graham-campbell/result-type updated from v1.0.2 to v1.0.3 patch See changes: https://github.com/GrahamCampbell/Result-Type/compare/v1.0.2...v1.0.3 Release notes: https://github.com/GrahamCampbell/Result-Type/releases/tag/v1.0.3
2021-10-24 03:16:44 +02:00
/** @var int<1,max> The maximum number of records that can be viewed per page for unauthenticated users. */
2022-07-01 09:41:04 +02:00
private int $maxPerPage = 25;
/** @var bool Whether the user is currently authenticated on this request. */
2023-06-06 12:43:06 +02:00
private bool $isAuthenticated;
/** @var bool Whether to show pagination controls. */
2022-07-01 09:41:04 +02:00
private bool $isDisabled = true;
/** @var callable|null A callable postprocessor that can be run on each result. */
2022-07-01 09:41:04 +02:00
private $postprocessor;
2022-05-31 13:41:35 +02:00
/**
* @param Pagerfanta<T> $paginator
*/
public function __construct(
2022-07-01 09:41:04 +02:00
private readonly Pagerfanta $paginator,
ServerRequest $request
) {
$this->router = $request->getRouter();
try {
$user = $request->getUser();
} catch (InvalidRequestAttribute) {
$user = null;
}
$this->isAuthenticated = ($user !== null);
$params = $request->getQueryParams();
$perPage = $params['rowCount'] ?? $params['per_page'] ?? null;
$currentPage = $params['current'] ?? $params['page'] ?? null;
if (null !== $perPage) {
$this->setPerPage((int)$perPage);
}
if (null !== $currentPage) {
$this->setCurrentPage((int)$currentPage);
}
}
public function getCurrentPage(): int
{
return $this->paginator->getCurrentPage();
}
public function setCurrentPage(int $currentPage): void
{
$this->paginator->setCurrentPage(
($currentPage >= 1) ? $currentPage : 1
);
}
public function setMaxPerPage(int $maxPerPage): void
{
$this->maxPerPage = ($maxPerPage > 0) ? $maxPerPage : 1;
$this->isDisabled = false;
}
public function getPerPage(): int
{
return $this->paginator->getMaxPerPage();
}
public function setPerPage(int $perPage): void
{
if ($perPage <= 0) {
$perPage = PHP_INT_MAX;
}
Dependency Update Changelogs summary: - pyrech/composer-changelogs updated from v1.8.0 to v1.8.1 patch See changes: https://github.com/pyrech/composer-changelogs/compare/v1.8.0...v1.8.1 Release notes: https://github.com/pyrech/composer-changelogs/releases/tag/v1.8.1 - james-heinrich/getid3 updated from dev-master@a440175 to dev-master@1e18087 See changes: https://github.com/JamesHeinrich/getID3/compare/a440175...1e18087 - doctrine/inflector updated from 2.0.3 to 2.0.4 patch See changes: https://github.com/doctrine/inflector/compare/2.0.3...2.0.4 Release notes: https://github.com/doctrine/inflector/releases/tag/2.0.4 - doctrine/common updated from 3.1.2 to 3.2.0 minor See changes: https://github.com/doctrine/common/compare/3.1.2...3.2.0 Release notes: https://github.com/doctrine/common/releases/tag/3.2.0 - doctrine/orm updated from 2.10.1 to 2.10.2 patch See changes: https://github.com/doctrine/orm/compare/2.10.1...2.10.2 Release notes: https://github.com/doctrine/orm/releases/tag/2.10.2 - behat/gherkin updated from v4.8.0 to v4.9.0 minor See changes: https://github.com/Behat/Gherkin/compare/v4.8.0...v4.9.0 Release notes: https://github.com/Behat/Gherkin/releases/tag/v4.9.0 - phpdocumentor/reflection-docblock updated from 5.2.2 to 5.3.0 minor See changes: https://github.com/phpDocumentor/ReflectionDocBlock/compare/5.2.2...5.3.0 Release notes: https://github.com/phpDocumentor/ReflectionDocBlock/releases/tag/5.3.0 - guzzlehttp/psr7 updated from 1.8.3 to 2.1.0 major See changes: https://github.com/guzzle/psr7/compare/1.8.3...2.1.0 Release notes: https://github.com/guzzle/psr7/releases/tag/2.1.0 - guzzlehttp/promises updated from 1.5.0 to 1.5.1 patch See changes: https://github.com/guzzle/promises/compare/1.5.0...1.5.1 Release notes: https://github.com/guzzle/promises/releases/tag/1.5.1 - guzzlehttp/guzzle updated from 7.3.0 to 7.4.0 minor See changes: https://github.com/guzzle/guzzle/compare/7.3.0...7.4.0 Release notes: https://github.com/guzzle/guzzle/releases/tag/7.4.0 - doctrine/migrations updated from 3.2.1 to 3.3.0 minor See changes: https://github.com/doctrine/migrations/compare/3.2.1...3.3.0 Release notes: https://github.com/doctrine/migrations/releases/tag/3.3.0 - gettext/gettext updated from v4.8.5 to v4.8.6 patch See changes: https://github.com/php-gettext/Gettext/compare/v4.8.5...v4.8.6 Release notes: https://github.com/php-gettext/Gettext/releases/tag/v4.8.6 - graham-campbell/guzzle-factory updated from v5.0.0 to v5.0.2 patch See changes: https://github.com/GrahamCampbell/Guzzle-Factory/compare/v5.0.0...v5.0.2 Release notes: https://github.com/GrahamCampbell/Guzzle-Factory/releases/tag/v5.0.2 - aws/aws-sdk-php updated from 3.198.0 to 3.199.2 minor See changes: https://github.com/aws/aws-sdk-php/compare/3.198.0...3.199.2 Release notes: https://github.com/aws/aws-sdk-php/releases/tag/3.199.2 - pagerfanta/core updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/core/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/core/releases/tag/v3.3.1 - pagerfanta/doctrine-collections-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-collections-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-collections-adapter/releases/tag/v3.3.1 - pagerfanta/doctrine-orm-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-orm-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-orm-adapter/releases/tag/v3.3.1 - ramsey/collection updated from 1.2.1 to 1.2.2 patch See changes: https://github.com/ramsey/collection/compare/1.2.1...1.2.2 Release notes: https://github.com/ramsey/collection/releases/tag/1.2.2 - squizlabs/php_codesniffer updated from 3.6.0 to 3.6.1 patch See changes: https://github.com/squizlabs/PHP_CodeSniffer/compare/3.6.0...3.6.1 Release notes: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.6.1 - phpstan/phpdoc-parser updated from 0.5.6 to 1.2.0 major See changes: https://github.com/phpstan/phpdoc-parser/compare/0.5.6...1.2.0 Release notes: https://github.com/phpstan/phpdoc-parser/releases/tag/1.2.0 - slevomat/coding-standard updated from 7.0.15 to 7.0.16 patch See changes: https://github.com/slevomat/coding-standard/compare/7.0.15...7.0.16 Release notes: https://github.com/slevomat/coding-standard/releases/tag/7.0.16 - egulias/email-validator updated from 3.1.1 to 3.1.2 patch See changes: https://github.com/egulias/EmailValidator/compare/3.1.1...3.1.2 Release notes: https://github.com/egulias/EmailValidator/releases/tag/3.1.2 - graham-campbell/result-type updated from v1.0.2 to v1.0.3 patch See changes: https://github.com/GrahamCampbell/Result-Type/compare/v1.0.2...v1.0.3 Release notes: https://github.com/GrahamCampbell/Result-Type/releases/tag/v1.0.3
2021-10-24 03:16:44 +02:00
/** @var int<1,max> $maxPerPage */
$maxPerPage = $this->isAuthenticated
? $perPage
: min($perPage, $this->maxPerPage);
$this->paginator->setMaxPerPage($maxPerPage);
$this->isDisabled = false;
}
public function setPostprocessor(callable $postprocessor): void
{
$this->postprocessor = $postprocessor;
}
public function isDisabled(): bool
{
return $this->isDisabled;
}
public function setIsDisabled(bool $isDisabled): void
{
$this->isDisabled = $isDisabled;
}
2022-11-05 01:20:42 +01:00
public function getIterator(): Generator
{
2022-11-05 01:20:42 +01:00
$iterator = $this->paginator->getIterator();
if ($this->postprocessor) {
foreach ($iterator as $row) {
yield ($this->postprocessor)($row, $this);
}
} else {
yield from $iterator;
}
}
public function count(): int
{
return $this->paginator->getNbResults();
}
public function write(Response $response): ResponseInterface
{
if ($this->isDisabled) {
Dependency Update Changelogs summary: - pyrech/composer-changelogs updated from v1.8.0 to v1.8.1 patch See changes: https://github.com/pyrech/composer-changelogs/compare/v1.8.0...v1.8.1 Release notes: https://github.com/pyrech/composer-changelogs/releases/tag/v1.8.1 - james-heinrich/getid3 updated from dev-master@a440175 to dev-master@1e18087 See changes: https://github.com/JamesHeinrich/getID3/compare/a440175...1e18087 - doctrine/inflector updated from 2.0.3 to 2.0.4 patch See changes: https://github.com/doctrine/inflector/compare/2.0.3...2.0.4 Release notes: https://github.com/doctrine/inflector/releases/tag/2.0.4 - doctrine/common updated from 3.1.2 to 3.2.0 minor See changes: https://github.com/doctrine/common/compare/3.1.2...3.2.0 Release notes: https://github.com/doctrine/common/releases/tag/3.2.0 - doctrine/orm updated from 2.10.1 to 2.10.2 patch See changes: https://github.com/doctrine/orm/compare/2.10.1...2.10.2 Release notes: https://github.com/doctrine/orm/releases/tag/2.10.2 - behat/gherkin updated from v4.8.0 to v4.9.0 minor See changes: https://github.com/Behat/Gherkin/compare/v4.8.0...v4.9.0 Release notes: https://github.com/Behat/Gherkin/releases/tag/v4.9.0 - phpdocumentor/reflection-docblock updated from 5.2.2 to 5.3.0 minor See changes: https://github.com/phpDocumentor/ReflectionDocBlock/compare/5.2.2...5.3.0 Release notes: https://github.com/phpDocumentor/ReflectionDocBlock/releases/tag/5.3.0 - guzzlehttp/psr7 updated from 1.8.3 to 2.1.0 major See changes: https://github.com/guzzle/psr7/compare/1.8.3...2.1.0 Release notes: https://github.com/guzzle/psr7/releases/tag/2.1.0 - guzzlehttp/promises updated from 1.5.0 to 1.5.1 patch See changes: https://github.com/guzzle/promises/compare/1.5.0...1.5.1 Release notes: https://github.com/guzzle/promises/releases/tag/1.5.1 - guzzlehttp/guzzle updated from 7.3.0 to 7.4.0 minor See changes: https://github.com/guzzle/guzzle/compare/7.3.0...7.4.0 Release notes: https://github.com/guzzle/guzzle/releases/tag/7.4.0 - doctrine/migrations updated from 3.2.1 to 3.3.0 minor See changes: https://github.com/doctrine/migrations/compare/3.2.1...3.3.0 Release notes: https://github.com/doctrine/migrations/releases/tag/3.3.0 - gettext/gettext updated from v4.8.5 to v4.8.6 patch See changes: https://github.com/php-gettext/Gettext/compare/v4.8.5...v4.8.6 Release notes: https://github.com/php-gettext/Gettext/releases/tag/v4.8.6 - graham-campbell/guzzle-factory updated from v5.0.0 to v5.0.2 patch See changes: https://github.com/GrahamCampbell/Guzzle-Factory/compare/v5.0.0...v5.0.2 Release notes: https://github.com/GrahamCampbell/Guzzle-Factory/releases/tag/v5.0.2 - aws/aws-sdk-php updated from 3.198.0 to 3.199.2 minor See changes: https://github.com/aws/aws-sdk-php/compare/3.198.0...3.199.2 Release notes: https://github.com/aws/aws-sdk-php/releases/tag/3.199.2 - pagerfanta/core updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/core/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/core/releases/tag/v3.3.1 - pagerfanta/doctrine-collections-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-collections-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-collections-adapter/releases/tag/v3.3.1 - pagerfanta/doctrine-orm-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-orm-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-orm-adapter/releases/tag/v3.3.1 - ramsey/collection updated from 1.2.1 to 1.2.2 patch See changes: https://github.com/ramsey/collection/compare/1.2.1...1.2.2 Release notes: https://github.com/ramsey/collection/releases/tag/1.2.2 - squizlabs/php_codesniffer updated from 3.6.0 to 3.6.1 patch See changes: https://github.com/squizlabs/PHP_CodeSniffer/compare/3.6.0...3.6.1 Release notes: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.6.1 - phpstan/phpdoc-parser updated from 0.5.6 to 1.2.0 major See changes: https://github.com/phpstan/phpdoc-parser/compare/0.5.6...1.2.0 Release notes: https://github.com/phpstan/phpdoc-parser/releases/tag/1.2.0 - slevomat/coding-standard updated from 7.0.15 to 7.0.16 patch See changes: https://github.com/slevomat/coding-standard/compare/7.0.15...7.0.16 Release notes: https://github.com/slevomat/coding-standard/releases/tag/7.0.16 - egulias/email-validator updated from 3.1.1 to 3.1.2 patch See changes: https://github.com/egulias/EmailValidator/compare/3.1.1...3.1.2 Release notes: https://github.com/egulias/EmailValidator/releases/tag/3.1.2 - graham-campbell/result-type updated from v1.0.2 to v1.0.3 patch See changes: https://github.com/GrahamCampbell/Result-Type/compare/v1.0.2...v1.0.3 Release notes: https://github.com/GrahamCampbell/Result-Type/releases/tag/v1.0.3
2021-10-24 03:16:44 +02:00
/** @var int<1,max> $maxPerPage */
$maxPerPage = PHP_INT_MAX;
$this->paginator->setCurrentPage(1);
Dependency Update Changelogs summary: - pyrech/composer-changelogs updated from v1.8.0 to v1.8.1 patch See changes: https://github.com/pyrech/composer-changelogs/compare/v1.8.0...v1.8.1 Release notes: https://github.com/pyrech/composer-changelogs/releases/tag/v1.8.1 - james-heinrich/getid3 updated from dev-master@a440175 to dev-master@1e18087 See changes: https://github.com/JamesHeinrich/getID3/compare/a440175...1e18087 - doctrine/inflector updated from 2.0.3 to 2.0.4 patch See changes: https://github.com/doctrine/inflector/compare/2.0.3...2.0.4 Release notes: https://github.com/doctrine/inflector/releases/tag/2.0.4 - doctrine/common updated from 3.1.2 to 3.2.0 minor See changes: https://github.com/doctrine/common/compare/3.1.2...3.2.0 Release notes: https://github.com/doctrine/common/releases/tag/3.2.0 - doctrine/orm updated from 2.10.1 to 2.10.2 patch See changes: https://github.com/doctrine/orm/compare/2.10.1...2.10.2 Release notes: https://github.com/doctrine/orm/releases/tag/2.10.2 - behat/gherkin updated from v4.8.0 to v4.9.0 minor See changes: https://github.com/Behat/Gherkin/compare/v4.8.0...v4.9.0 Release notes: https://github.com/Behat/Gherkin/releases/tag/v4.9.0 - phpdocumentor/reflection-docblock updated from 5.2.2 to 5.3.0 minor See changes: https://github.com/phpDocumentor/ReflectionDocBlock/compare/5.2.2...5.3.0 Release notes: https://github.com/phpDocumentor/ReflectionDocBlock/releases/tag/5.3.0 - guzzlehttp/psr7 updated from 1.8.3 to 2.1.0 major See changes: https://github.com/guzzle/psr7/compare/1.8.3...2.1.0 Release notes: https://github.com/guzzle/psr7/releases/tag/2.1.0 - guzzlehttp/promises updated from 1.5.0 to 1.5.1 patch See changes: https://github.com/guzzle/promises/compare/1.5.0...1.5.1 Release notes: https://github.com/guzzle/promises/releases/tag/1.5.1 - guzzlehttp/guzzle updated from 7.3.0 to 7.4.0 minor See changes: https://github.com/guzzle/guzzle/compare/7.3.0...7.4.0 Release notes: https://github.com/guzzle/guzzle/releases/tag/7.4.0 - doctrine/migrations updated from 3.2.1 to 3.3.0 minor See changes: https://github.com/doctrine/migrations/compare/3.2.1...3.3.0 Release notes: https://github.com/doctrine/migrations/releases/tag/3.3.0 - gettext/gettext updated from v4.8.5 to v4.8.6 patch See changes: https://github.com/php-gettext/Gettext/compare/v4.8.5...v4.8.6 Release notes: https://github.com/php-gettext/Gettext/releases/tag/v4.8.6 - graham-campbell/guzzle-factory updated from v5.0.0 to v5.0.2 patch See changes: https://github.com/GrahamCampbell/Guzzle-Factory/compare/v5.0.0...v5.0.2 Release notes: https://github.com/GrahamCampbell/Guzzle-Factory/releases/tag/v5.0.2 - aws/aws-sdk-php updated from 3.198.0 to 3.199.2 minor See changes: https://github.com/aws/aws-sdk-php/compare/3.198.0...3.199.2 Release notes: https://github.com/aws/aws-sdk-php/releases/tag/3.199.2 - pagerfanta/core updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/core/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/core/releases/tag/v3.3.1 - pagerfanta/doctrine-collections-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-collections-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-collections-adapter/releases/tag/v3.3.1 - pagerfanta/doctrine-orm-adapter updated from v3.3.0 to v3.3.1 patch See changes: https://github.com/Pagerfanta/doctrine-orm-adapter/compare/pagerfanta-packages:v3.3.0...Pagerfanta:v3.3.1 Release notes: https://github.com/Pagerfanta/doctrine-orm-adapter/releases/tag/v3.3.1 - ramsey/collection updated from 1.2.1 to 1.2.2 patch See changes: https://github.com/ramsey/collection/compare/1.2.1...1.2.2 Release notes: https://github.com/ramsey/collection/releases/tag/1.2.2 - squizlabs/php_codesniffer updated from 3.6.0 to 3.6.1 patch See changes: https://github.com/squizlabs/PHP_CodeSniffer/compare/3.6.0...3.6.1 Release notes: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.6.1 - phpstan/phpdoc-parser updated from 0.5.6 to 1.2.0 major See changes: https://github.com/phpstan/phpdoc-parser/compare/0.5.6...1.2.0 Release notes: https://github.com/phpstan/phpdoc-parser/releases/tag/1.2.0 - slevomat/coding-standard updated from 7.0.15 to 7.0.16 patch See changes: https://github.com/slevomat/coding-standard/compare/7.0.15...7.0.16 Release notes: https://github.com/slevomat/coding-standard/releases/tag/7.0.16 - egulias/email-validator updated from 3.1.1 to 3.1.2 patch See changes: https://github.com/egulias/EmailValidator/compare/3.1.1...3.1.2 Release notes: https://github.com/egulias/EmailValidator/releases/tag/3.1.2 - graham-campbell/result-type updated from v1.0.2 to v1.0.3 patch See changes: https://github.com/GrahamCampbell/Result-Type/compare/v1.0.2...v1.0.3 Release notes: https://github.com/GrahamCampbell/Result-Type/releases/tag/v1.0.3
2021-10-24 03:16:44 +02:00
$this->paginator->setMaxPerPage($maxPerPage);
}
$total = $this->count();
$totalPages = $this->paginator->getNbPages();
2022-11-05 01:20:42 +01:00
$results = iterator_to_array($this->getIterator(), false);
if ($this->isDisabled) {
return $response->withJson($results);
}
$pageLinks = [];
$pageLinks['first'] = $this->router->fromHereWithQuery(null, [], ['page' => 1]);
2022-05-08 20:05:02 +02:00
$prevPage = $this->paginator->hasPreviousPage()
? $this->paginator->getPreviousPage()
: 1;
$pageLinks['previous'] = $this->router->fromHereWithQuery(null, [], ['page' => $prevPage]);
2022-05-08 20:05:02 +02:00
$nextPage = $this->paginator->hasNextPage()
? $this->paginator->getNextPage()
: $this->paginator->getNbPages();
$pageLinks['next'] = $this->router->fromHereWithQuery(null, [], ['page' => $nextPage]);
$pageLinks['last'] = $this->router->fromHereWithQuery(null, [], ['page' => $totalPages]);
return $response->withJson(
[
'page' => $this->getCurrentPage(),
'per_page' => $this->getPerPage(),
'total' => $total,
'total_pages' => $totalPages,
'links' => $pageLinks,
'rows' => $results,
]
);
}
/**
* @template X of mixed
*
* @param AdapterInterface<X> $adapter
* @return static<array-key, X>
*/
public static function fromAdapter(
AdapterInterface $adapter,
ServerRequest $request
): self {
return new self(
new Pagerfanta($adapter),
$request
);
}
2022-05-31 13:41:35 +02:00
/**
* @template XKey of array-key
* @template X of mixed
*
* @param array<XKey, X> $input
* @return static<XKey, X>
*/
public static function fromArray(array $input, ServerRequest $request): self
{
return self::fromAdapter(new ArrayAdapter($input), $request);
}
2022-05-31 13:41:35 +02:00
/**
* @template XKey of array-key
* @template X of mixed
*
* @param Collection<XKey, X> $collection
* @return static<XKey, X>
*/
public static function fromCollection(Collection $collection, ServerRequest $request): self
{
return self::fromAdapter(new CollectionAdapter($collection), $request);
}
2022-05-31 13:41:35 +02:00
/**
* @return static<int, mixed>
*/
public static function fromQueryBuilder(QueryBuilder $qb, ServerRequest $request): self
{
return self::fromAdapter(new QueryAdapter($qb), $request);
}
2022-05-31 13:41:35 +02:00
/**
* @return static<int, mixed>
*/
public static function fromQuery(Query $query, ServerRequest $request): self
{
return self::fromAdapter(new QueryAdapter($query), $request);
}
}