Skip to content

Commit

Permalink
Merge pull request #18119 from cakephp/feat/6.0-paginated-interface
Browse files Browse the repository at this point in the history
Make PaginatedInterface extend IteratorAggregrate instead of Traversable
  • Loading branch information
markstory authored Jan 6, 2025
2 parents 49e9c39 + 327c533 commit cb0bdf3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
13 changes: 8 additions & 5 deletions src/Datasource/Paging/PaginatedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
namespace Cake\Datasource\Paging;

use Countable;
use IteratorAggregate;
use Traversable;

/**
* This interface describes the methods for pagination instance.
* This interface describes the methods for paginated instance.
*
* @template-extends \Traversable<mixed>
* @template TKey
* @template TValue
* @template-extends \IteratorAggregate<TKey, TValue>
*/
interface PaginatedInterface extends Countable, Traversable
interface PaginatedInterface extends Countable, IteratorAggregate
{
/**
* Get current page number.
Expand Down Expand Up @@ -71,9 +74,9 @@ public function hasNextPage(): bool;
/**
* Get paginated items.
*
* @return iterable
* @return \Traversable<TKey, TValue>
*/
public function items(): iterable;
public function items(): Traversable;

/**
* Get paging param.
Expand Down
30 changes: 19 additions & 11 deletions src/Datasource/Paging/PaginatedResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
*/
namespace Cake\Datasource\Paging;

use IteratorAggregate;
use ArrayIterator;
use JsonSerializable;
use Traversable;

/**
* Paginated resultset.
*
* @template T of mixed
* @template-implements \IteratorAggregate<mixed>
* @template TKey
* @template TValue
* @template-implements \Cake\Datasource\Paging\PaginatedInterface<TKey, TValue>
*/
class PaginatedResultSet implements IteratorAggregate, JsonSerializable, PaginatedInterface
class PaginatedResultSet implements JsonSerializable, PaginatedInterface
{
/**
* Resultset instance.
*
* @var \Traversable<T>
* @var \Traversable<TKey, TValue>
*/
protected Traversable $results;

Expand All @@ -45,13 +46,18 @@ class PaginatedResultSet implements IteratorAggregate, JsonSerializable, Paginat
/**
* Constructor
*
* @param \Traversable<T> $results Resultset instance.
* @param iterable $results Resultset instance.
* @param array $params Paging params.
*/
public function __construct(Traversable $results, array $params)
public function __construct(iterable $results, array $params)
{
$this->results = $results;
$this->results = is_array($results) ? new ArrayIterator($results) : $results;

$this->params = $params;

if (!isset($this->params['count']) && is_countable($results)) {
$this->params['count'] = count($results);
}
}

/**
Expand All @@ -67,7 +73,7 @@ public function count(): int
*
* This will exhaust the iterator `items`.
*
* @return array<array-key, T>
* @return array<array-key, TValue>
*/
public function toArray(): array
{
Expand All @@ -77,7 +83,7 @@ public function toArray(): array
/**
* Get paginated items.
*
* @return \Traversable<T> The paginated items result set.
* @return \Traversable<TKey, TValue> The paginated items result set.
*/
public function items(): Traversable
{
Expand Down Expand Up @@ -159,7 +165,9 @@ public function pagingParams(): array
}

/**
* @inheritDoc
* Get inner iterator
*
* @return \Traversable<TKey, TValue>
*/
public function getIterator(): Traversable
{
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Datasource/Paging/PaginatedResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@

class PaginatedResultSetTest extends TestCase
{
public function testConstructor(): void
{
$array = ['a' => 'a', 'b' => 'b', 'c' => 'c'];
$paginatedResults = new PaginatedResultSet(
$array,
[],
);

$result = iterator_to_array($paginatedResults);
$this->assertsame($array, $result);
$this->assertSame(3, $paginatedResults->count());

$collection = new Collection($array);
$paginatedResults = new PaginatedResultSet(
$collection,
[],
);
$result = iterator_to_array($paginatedResults);
$this->assertsame($array, $result);
$this->assertSame(3, $paginatedResults->count());
}

public function testItems(): void
{
$resultSet = new class ([]) extends ResultSet {
Expand Down

0 comments on commit cb0bdf3

Please sign in to comment.