Skip to content

Commit

Permalink
feat: Add NormalizeIteratorAggregate.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 25, 2022
1 parent 4c0a797 commit 24d2e8c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/NormalizeIteratorAggregate.php
@@ -0,0 +1,46 @@
<?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 IteratorAggregate;
use Traversable;

/**
* @template TKey
* @template T
*
* @implements IteratorAggregate<int, T>
*/
final class NormalizeIteratorAggregate implements IteratorAggregate
{
/**
* @var iterable<TKey, T>
*/
private iterable $iterable;

/**
* @param iterable<TKey, T> $iterable
*/
public function __construct(iterable $iterable)
{
$this->iterable = $iterable;
}

/**
* @return Generator<int, T>
*/
public function getIterator(): Traversable
{
foreach ($this->iterable as $c) {
yield $c;
}
}
}
32 changes: 32 additions & 0 deletions tests/unit/NormalizeIteratorAggregateTest.php
@@ -0,0 +1,32 @@
<?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 loophp\iterators\NormalizeIteratorAggregate;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversDefaultClass \loophp\iterators
*/
final class NormalizeIteratorAggregateTest extends TestCase
{
public function testBasic(): void
{
$input = static function () {
yield from array_combine(range('a', 'c'), range('a', 'c'));
};

$iterator = new NormalizeIteratorAggregate($input());

$expected = ['a', 'b', 'c'];
self::assertSame($expected, iterator_to_array($iterator));
}
}

0 comments on commit 24d2e8c

Please sign in to comment.