Skip to content

Commit

Permalink
Add traversable paginator (#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
toby-griffiths committed Feb 17, 2021
1 parent 959f082 commit 3557329
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
* Doctrine: Better exception to find which resource is linked to an exception (#3965)
* Doctrine: Allow mixed type value for date filter (notice if invalid) (#3870)
* MongoDB: `date_immutable` support (#3940)
* Add `TraversablePaginator` (#3783)

## 2.6.3

Expand Down
90 changes: 90 additions & 0 deletions src/DataProvider/TraversablePaginator.php
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\DataProvider;

final class TraversablePaginator implements \IteratorAggregate, PaginatorInterface
{
private $traversable;
private $currentPage;
private $itemsPerPage;
private $totalItems;

public function __construct(\Traversable $iterator, float $currentPage, float $itemsPerPage, float $totalItems)
{
$this->traversable = $iterator;
$this->currentPage = $currentPage;
$this->itemsPerPage = $itemsPerPage;
$this->totalItems = $totalItems;
}

/**
* {@inheritdoc}
*/
public function getCurrentPage(): float
{
return $this->currentPage;
}

/**
* {@inheritdoc}
*/
public function getLastPage(): float
{
if (0. >= $this->itemsPerPage) {
return 1.;
}

return max(ceil($this->totalItems / $this->itemsPerPage) ?: 1., 1.);
}

/**
* {@inheritdoc}
*/
public function getItemsPerPage(): float
{
return $this->itemsPerPage;
}

/**
* {@inheritdoc}
*/
public function getTotalItems(): float
{
return $this->totalItems;
}

/**
* {@inheritdoc}
*/
public function count(): int
{
if ($this->getCurrentPage() < $this->getLastPage()) {
return (int) ceil($this->itemsPerPage);
}

if (0. >= $this->itemsPerPage) {
return (int) ceil($this->totalItems);
}

return $this->totalItems % $this->itemsPerPage;
}

/**
* {@inheritdoc}
*/
public function getIterator(): \Traversable
{
return $this->traversable;
}
}
56 changes: 56 additions & 0 deletions tests/DataProvider/TraversablePaginatorTest.php
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\DataProvider;

use ApiPlatform\Core\DataProvider\TraversablePaginator;
use ArrayIterator;
use PHPUnit\Framework\TestCase;

class TraversablePaginatorTest extends TestCase
{
/**
* @dataProvider initializeProvider
*/
public function testInitialize(
array $results,
float $currentPage,
float $perPage,
float $totalItems,
float $lastPage,
float $currentItems
): void {
$traversable = new ArrayIterator($results);

$paginator = new TraversablePaginator($traversable, $currentPage, $perPage, $totalItems);

self::assertEquals($totalItems, $paginator->getTotalItems());
self::assertEquals($currentPage, $paginator->getCurrentPage());
self::assertEquals($lastPage, $paginator->getLastPage());
self::assertEquals($perPage, $paginator->getItemsPerPage());
self::assertEquals($currentItems, $paginator->count());

self::assertSame($results, iterator_to_array($paginator));
}

public function initializeProvider(): array
{
return [
'First of three pages of 3 items each' => [[0, 1, 2, 3, 4, 5, 6], 1, 3, 7, 3, 3],
'Second of two pages of 3 items for the first page and 2 for the second' => [[0, 1, 2, 3, 4], 2, 3, 5, 2, 2],
'Empty results' => [[], 1, 2, 0, 1, 0],
'0 items per page' => [[0, 1, 2, 3], 1, 0, 4, 1, 4],
'Total items less than items per page' => [[0, 1, 2], 1, 4, 3, 1, 3],
];
}
}

0 comments on commit 3557329

Please sign in to comment.