Skip to content

Commit

Permalink
Fix array and iterable support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moln committed May 16, 2023
1 parent 2fd6767 commit a133d0a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/.idea
/vendor
.phpunit.result.cache
composer.lock
6 changes: 5 additions & 1 deletion src/Paginator/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public function __construct(iterable $data, int $count, int $currentPage = 1, in

public function getIterator(): Traversable
{
return new \ArrayIterator($this->data);
if (is_array($this->data)) {
return new \ArrayIterator($this->data);
} else {
return new \IteratorIterator($this->data);
}
}

public function count(): int
Expand Down
25 changes: 22 additions & 3 deletions test/Basic/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,31 @@
class CollectionNormalizerTest extends TestCase
{

public function testNormalize()

public function iterableData()
{
$data = new Paginator(
$iter = (function() {
yield (['id' => 1, 'name' => 'aaa']);
})();
return [
[
['id' => 1, 'name' => 'aaa'],
$iter,
],
[
[
['id' => 1, 'name' => 'aaa'],
]
]
];
}

/**
* @dataProvider iterableData
*/
public function testNormalize(iterable $iter)
{
$data = new Paginator(
$iter,
100, 1, 10
);

Expand Down

0 comments on commit a133d0a

Please sign in to comment.