diff --git a/.gitignore b/.gitignore index a09c56d..5a179d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /.idea +/vendor +.phpunit.result.cache +composer.lock \ No newline at end of file diff --git a/src/Paginator/Paginator.php b/src/Paginator/Paginator.php index 73f9846..c84277d 100644 --- a/src/Paginator/Paginator.php +++ b/src/Paginator/Paginator.php @@ -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 diff --git a/test/Basic/CollectionNormalizerTest.php b/test/Basic/CollectionNormalizerTest.php index 7890b81..d247ac6 100644 --- a/test/Basic/CollectionNormalizerTest.php +++ b/test/Basic/CollectionNormalizerTest.php @@ -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 );