Skip to content

Commit a133d0a

Browse files
committed
Fix array and iterable support.
1 parent 2fd6767 commit a133d0a

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/.idea
2+
/vendor
3+
.phpunit.result.cache
4+
composer.lock

src/Paginator/Paginator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public function __construct(iterable $data, int $count, int $currentPage = 1, in
2424

2525
public function getIterator(): Traversable
2626
{
27-
return new \ArrayIterator($this->data);
27+
if (is_array($this->data)) {
28+
return new \ArrayIterator($this->data);
29+
} else {
30+
return new \IteratorIterator($this->data);
31+
}
2832
}
2933

3034
public function count(): int

test/Basic/CollectionNormalizerTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,31 @@
1515
class CollectionNormalizerTest extends TestCase
1616
{
1717

18-
public function testNormalize()
18+
19+
public function iterableData()
1920
{
20-
$data = new Paginator(
21+
$iter = (function() {
22+
yield (['id' => 1, 'name' => 'aaa']);
23+
})();
24+
return [
2125
[
22-
['id' => 1, 'name' => 'aaa'],
26+
$iter,
2327
],
28+
[
29+
[
30+
['id' => 1, 'name' => 'aaa'],
31+
]
32+
]
33+
];
34+
}
35+
36+
/**
37+
* @dataProvider iterableData
38+
*/
39+
public function testNormalize(iterable $iter)
40+
{
41+
$data = new Paginator(
42+
$iter,
2443
100, 1, 10
2544
);
2645

0 commit comments

Comments
 (0)