Skip to content

Commit dde7f26

Browse files
committed
Change Result - return Iterator, not Traversable
1 parent b2da04b commit dde7f26

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

spec/Paginator/Loader/ResultSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function it_iterates_over_array()
3232
$this->count()->shouldBe(count($data));
3333
}
3434

35-
function it_iterates_over_traversable()
35+
function it_iterates_over_iterator()
3636
{
3737
$data = [1,2,3,4,5];
3838
$traversable = new ArrayIterator($data);

src/Paginator/Loader/Result.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
final class Result implements IteratorAggregate, Countable
1313
{
1414
/**
15-
* @var Traversable
15+
* @var Iterator
1616
*/
1717
private $iterator;
1818

@@ -21,7 +21,7 @@ final class Result implements IteratorAggregate, Countable
2121
*/
2222
private $count;
2323

24-
public function __construct(Traversable $items, int $count)
24+
public function __construct(Iterator $items, int $count)
2525
{
2626
$this->iterator = $items;
2727
$this->count = $count;
@@ -33,8 +33,8 @@ public static function fromIterable(iterable $items): self
3333
return self::fromArray($items);
3434
}
3535

36-
if ($items instanceof Traversable) {
37-
return self::fromTraversable($items);
36+
if ($items instanceof Iterator) {
37+
return self::fromIterator($items);
3838
}
3939

4040
throw new InvalidArgumentException('Items should be array or Traversable');
@@ -48,13 +48,13 @@ public static function fromArray(array $items): self
4848
);
4949
}
5050

51-
public static function fromTraversable(Traversable $items): self
51+
public static function fromIterator(Iterator $items): self
5252
{
5353
$count = self::countItems($items);
5454
return new self($items, $count);
5555
}
5656

57-
public function getIterator(): Traversable
57+
public function getIterator(): Iterator
5858
{
5959
return $this->iterator;
6060
}
@@ -64,7 +64,7 @@ public function count(): int
6464
return $this->count;
6565
}
6666

67-
private static function countItems(Traversable $items): int
67+
private static function countItems(Iterator $items): int
6868
{
6969
if ($items instanceof Countable) {
7070
$count = $items->count();
@@ -73,9 +73,7 @@ private static function countItems(Traversable $items): int
7373
foreach ($items as $v) {
7474
++$count;
7575
}
76-
if ($items instanceof Iterator) {
77-
$items->rewind();
78-
}
76+
$items->rewind();
7977
}
8078

8179
return $count;

0 commit comments

Comments
 (0)