Skip to content

Commit 31f4684

Browse files
committed
CachingIterator: refactoring
1 parent 09d980b commit 31f4684

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

src/Iterators/CachingIterator.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,12 @@ class CachingIterator extends \CachingIterator implements \Countable
3131
private int $counter = 0;
3232

3333

34-
public function __construct($iterator)
34+
public function __construct(iterable|\stdClass $iterable)
3535
{
36-
if (is_array($iterator) || $iterator instanceof \stdClass) {
37-
$iterator = new \ArrayIterator($iterator);
38-
39-
} elseif ($iterator instanceof \IteratorAggregate) {
40-
do {
41-
$iterator = $iterator->getIterator();
42-
} while ($iterator instanceof \IteratorAggregate);
43-
44-
assert($iterator instanceof \Iterator);
45-
46-
} elseif ($iterator instanceof \Iterator) {
47-
} elseif ($iterator instanceof \Traversable) {
48-
$iterator = new \IteratorIterator($iterator);
49-
} else {
50-
throw new Nette\InvalidArgumentException(sprintf('Invalid argument passed to %s; array or Traversable expected, %s given.', self::class, get_debug_type($iterator)));
51-
}
52-
53-
parent::__construct($iterator, 0);
36+
$iterable = $iterable instanceof \stdClass
37+
? new \ArrayIterator($iterable)
38+
: Nette\Utils\Iterables::toIterator($iterable);
39+
parent::__construct($iterable, 0);
5440
}
5541

5642

tests/Iterators/CachingIterator.construct.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test('object', function () {
8686
Assert::exception(function () {
8787
$arr = dir('.');
8888
foreach (new Iterators\CachingIterator($arr) as $k => $v);
89-
}, InvalidArgumentException::class, null);
89+
}, TypeError::class, null);
9090
});
9191

9292

0 commit comments

Comments
 (0)