Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
959f082
commit 3557329
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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], | ||
]; | ||
} | ||
} |