-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
InfiniteIteratorAggregate
.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\iterators; | ||
|
||
use Generator; | ||
use Iterator; | ||
use IteratorAggregate; | ||
use Traversable; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
* | ||
* @implements IteratorAggregate<TKey, T> | ||
*/ | ||
final class InfiniteIteratorAggregate implements IteratorAggregate | ||
{ | ||
/** | ||
* @var CachingIteratorAggregate<TKey, T> | ||
*/ | ||
private CachingIteratorAggregate $iterator; | ||
|
||
/** | ||
* @param Iterator<TKey, T> $iterator | ||
*/ | ||
public function __construct(Iterator $iterator) | ||
{ | ||
$this->iterator = new CachingIteratorAggregate($iterator); | ||
} | ||
|
||
/** | ||
* @return Generator<TKey, T> | ||
*/ | ||
public function getIterator(): Traversable | ||
{ | ||
yield from $this->iterator; | ||
|
||
yield from $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests\loophp\iterators; | ||
|
||
use LimitIterator; | ||
use loophp\iterators\InfiniteIteratorAggregate; | ||
use loophp\iterators\NormalizeIteratorAggregate; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @coversDefaultClass \loophp\iterators | ||
*/ | ||
final class InfiniteIteratorAggregateTest extends TestCase | ||
{ | ||
public function testBasic(): void | ||
{ | ||
$input = static function () { | ||
yield from range('a', 'c'); | ||
}; | ||
|
||
$iterator = new NormalizeIteratorAggregate( | ||
new LimitIterator( | ||
(new InfiniteIteratorAggregate($input()))->getIterator(), | ||
0, | ||
5 | ||
) | ||
); | ||
|
||
$expected = ['a', 'b', 'c', 'a', 'b']; | ||
self::assertSame($expected, iterator_to_array($iterator)); | ||
} | ||
} |