Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pagerfanta/pagerfanta to properly display pagination #14

Merged
merged 9 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ sudo: required

language: php
php:
- 7.1
- 7.2
- 7.3

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": "~7.1.0|~7.2.0|~7.3.0",
"php": "~7.2.0|~7.3.0",
"ext-pcntl": "*",
"symfony/dependency-injection": "^3.3",
"symfony/config": "^3.3",
Expand All @@ -32,7 +32,9 @@
"symfony/property-info": "^4.3",
"doctrine/annotations": "^1.8",
"ramsey/uuid": "^3.8",
"webgriffe/amp-elasticsearch": "^1.1"
"webgriffe/amp-elasticsearch": "^1.1",
"pagerfanta/pagerfanta": "^2.3",
"symfony/deprecation-contracts": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ parameters:
- '/Method Webgriffe\\Esb\\FlowExtension::getNamespace\(\) should return string but returns false\./'
- '/Method Webgriffe\\Esb\\FlowExtension::getXsdValidationBasePath\(\) should return string but returns false\./'
- '/Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::useAttributeAsKey\(\)\./'
- '/Webgriffe\\Esb\\Console\\Pager\\AsyncPager::__construct\(\) does not call parent constructor from Pagerfanta\\Pagerfanta\./'
2 changes: 2 additions & 0 deletions src/Console/Pager/AmpElasticsearchUriSearchAdapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Webgriffe\Esb\Console\Pager;

use Amp\Promise;
Expand Down
29 changes: 18 additions & 11 deletions src/Console/Pager/AsyncPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
namespace Webgriffe\Esb\Console\Pager;

use Amp\Promise;
use Pagerfanta\Exception\LessThan1CurrentPageException;
use Pagerfanta\Exception\LessThan1MaxPerPageException;
use Pagerfanta\Exception\NotValidCurrentPageException;
use Pagerfanta\Exception\NotValidMaxPerPageException;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Pagerfanta\Pagerfanta;
use Webmozart\Assert\Assert;
use function Amp\call;

/**
* @internal
*/
class AsyncPager
class AsyncPager extends Pagerfanta
{
/**
* @var AsyncPagerAdapterInterface
Expand Down Expand Up @@ -45,6 +52,10 @@ class AsyncPager
*/
private $currentPageResults;

/**
* @noinspection MagicMethodsValidityInspection
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(AsyncPagerAdapterInterface $adapter, int $maxPerPage = 10, int $currentPage = 1)
{
$this->adapter = $adapter;
Expand Down Expand Up @@ -124,9 +135,7 @@ public function getNormalizeOutOfRangePages()

private function filterBoolean($value)
{
if (!is_bool($value)) {
throw new NotBooleanException();
}
Assert::boolean($value);

return $value;
}
Expand All @@ -140,7 +149,7 @@ private function filterBoolean($value)
*
* @return self
*
* @throws NotIntegerMaxPerPageException If the max per page is not an integer even converting.
* @throws NotValidMaxPerPageException If the max per page is not an integer even converting.
* @throws LessThan1MaxPerPageException If the max per page is less than 1.
*/
public function setMaxPerPage($maxPerPage)
Expand All @@ -162,7 +171,7 @@ private function filterMaxPerPage($maxPerPage)
private function checkMaxPerPage($maxPerPage)
{
if (!is_int($maxPerPage)) {
throw new NotIntegerMaxPerPageException();
throw new NotValidMaxPerPageException();
}

if ($maxPerPage < 1) {
Expand Down Expand Up @@ -195,7 +204,7 @@ public function getMaxPerPage()
*
* @return self
*
* @throws NotIntegerCurrentPageException If the current page is not an integer even converting.
* @throws NotValidCurrentPageException If the current page is not an integer even converting.
* @throws LessThan1CurrentPageException If the current page is less than 1.
* @throws OutOfRangeCurrentPageException If It is not allowed out of range pages and they are not normalized.
*/
Expand Down Expand Up @@ -250,7 +259,7 @@ private function filterCurrentPage($currentPage)
private function checkCurrentPage($currentPage)
{
if (!is_int($currentPage)) {
throw new NotIntegerCurrentPageException();
throw new NotValidCurrentPageException();
}

if ($currentPage < 1) {
Expand Down Expand Up @@ -534,9 +543,7 @@ private function needsToIntegerConversion($value)
*/
public function getPageNumberForItemAtPosition($position)
{
if (!is_int($position)) {
throw new NotIntegerException();
}
Assert::integer($position);

if ($this->getNbResults() < $position) {
throw new \OutOfBoundsException(sprintf(
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Pager/AsyncPagerAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function getNbResults(): Promise;
/**
* Returns an slice of the results.
*
* @param integer $offset The offset.
* @param integer $length The length.
* @param int $offset The offset.
* @param int $length The length.
*
* @return Promise which resolve array|\Traversable The slice.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Console/Pager/LessThan1CurrentPageException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" class is deprecated and will be removed in 3.0. Use the "%s" class instead.',
LessThan1CurrentPageException::class,
\Pagerfanta\Exception\LessThan1CurrentPageException::class
);

/**
* @deprecated to be removed in 3.0
*/
final class LessThan1CurrentPageException extends NotValidCurrentPageException
{
}
9 changes: 9 additions & 0 deletions src/Console/Pager/LessThan1MaxPerPageException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" class is deprecated and will be removed in 3.0. Use the "%s" class instead.',
LessThan1MaxPerPageException::class,
\Pagerfanta\Exception\LessThan1MaxPerPageException::class
);

final class LessThan1MaxPerPageException extends NotValidMaxPerPageException
{
}
11 changes: 11 additions & 0 deletions src/Console/Pager/NotBooleanException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" exception is deprecated and will be removed in 3.0.',
NotBooleanException::class
);

/**
* @deprecated to be removed in 3.0
*/
final class NotBooleanException extends \InvalidArgumentException
{
}
11 changes: 11 additions & 0 deletions src/Console/Pager/NotIntegerCurrentPageException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" exception is deprecated and will be removed in 3.0.',
NotIntegerCurrentPageException::class
);

/**
* @deprecated to be removed in 3.0
*/
final class NotIntegerCurrentPageException extends NotValidCurrentPageException
{

Expand Down
11 changes: 11 additions & 0 deletions src/Console/Pager/NotIntegerException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" exception is deprecated and will be removed in 3.0.',
NotIntegerException::class
);

/**
* @deprecated to be removed in 3.0
*/
final class NotIntegerException extends \InvalidArgumentException
{
}
11 changes: 11 additions & 0 deletions src/Console/Pager/NotIntegerMaxPerPageException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" exception is deprecated and will be removed in 3.0.',
NotIntegerMaxPerPageException::class
);

/**
* @deprecated to be removed in 3.0
*/
final class NotIntegerMaxPerPageException extends NotValidMaxPerPageException
{
}
9 changes: 9 additions & 0 deletions src/Console/Pager/NotValidCurrentPageException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" exception is deprecated and will be removed in 3.0.',
NotValidCurrentPageException::class
);

/**
* @internal
* @deprecated to be removed in 3.0
*/
class NotValidCurrentPageException extends \InvalidArgumentException
{
Expand Down
9 changes: 9 additions & 0 deletions src/Console/Pager/NotValidMaxPerPageException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" exception is deprecated and will be removed in 3.0.',
NotValidMaxPerPageException::class
);

/**
* @internal
* @deprecated to be removed in 3.0
*/
class NotValidMaxPerPageException extends \InvalidArgumentException
{
Expand Down
12 changes: 12 additions & 0 deletions src/Console/Pager/OutOfRangeCurrentPageException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
<?php
// @codingStandardsIgnoreFile

namespace Webgriffe\Esb\Console\Pager;

trigger_deprecation(
'webgriffe/esb',
'2.2',
'The "%s" class is deprecated and will be removed in 3.0. Use the "%s" class instead.',
OutOfRangeCurrentPageException::class,
\Pagerfanta\Exception\OutOfRangeCurrentPageException::class
);

/**
* @deprecated to be removed in 3.0
*/
final class OutOfRangeCurrentPageException extends NotValidCurrentPageException
{
}
42 changes: 42 additions & 0 deletions src/Console/PagerfantaTwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Webgriffe\Esb\Console;

use Pagerfanta\Pagerfanta;
use Pagerfanta\View\ViewInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class PagerfantaTwigExtension extends AbstractExtension
{
/**
* @var ViewInterface
*/
private $view;

public function __construct(ViewInterface $view)
{
$this->view = $view;
}

public function getFunctions()
{
return [
new TwigFunction('pagerfanta', [$this, 'renderPagerfanta'], ['is_safe' => ['html']])
];
}

public function renderPagerfanta(
Pagerfanta $pagerfanta,
string $route,
array $options = []
): string {
$routeGenerator = function ($page) use ($route): string {
return strtr($route, ['{page}' => $page]);
};

return $this->view->render($pagerfanta, $routeGenerator, $options);
}
}
9 changes: 9 additions & 0 deletions src/Console/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ services:
console.twig.class_extension:
class: \Webgriffe\Esb\Console\ClassTwigExtension

console.twig.pagerfanta_extension:
class: \Webgriffe\Esb\Console\PagerfantaTwigExtension
arguments:
- '@console.pagerfanta.view.bootstrap4'

console.twig:
class: \Twig\Environment
arguments:
- '@console.twig.loader'
calls:
- ['addExtension', ['@console.twig.class_extension']]
- ['addExtension', ['@console.twig.pagerfanta_extension']]

console.server:
class: \Webgriffe\Esb\Console\Server
Expand All @@ -37,3 +43,6 @@ services:
calls:
- [ setContainer, [ '@service_container' ]]
public: true

console.pagerfanta.view.bootstrap4:
class: \Pagerfanta\View\TwitterBootstrap4View
Loading