Skip to content

Commit

Permalink
feat: Add hasNext method to Caching iterator aggregates.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Mar 13, 2022
1 parent e2cf3ea commit 0d80e09
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/CachingIteratorAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,28 @@
final class CachingIteratorAggregate implements IteratorAggregate
{
/**
* @var UnpackIterableAggregate<TKey, T>
* @var SimpleCachingIteratorAggregate<int, array{0: TKey, 1:T}>
*/
private UnpackIterableAggregate $iteratorAggregate;
private SimpleCachingIteratorAggregate $cachingIterator;

/**
* @param Iterator<TKey, T> $iterator
*/
public function __construct(Iterator $iterator)
{
$this->iteratorAggregate = (new UnpackIterableAggregate(new SimpleCachingIteratorAggregate((new PackIterableAggregate($iterator))->getIterator())));
$this->cachingIterator = new SimpleCachingIteratorAggregate((new PackIterableAggregate($iterator))->getIterator());
}

/**
* @return Generator<TKey, T>
*/
public function getIterator(): Generator
{
yield from $this->iteratorAggregate->getIterator();
yield from (new UnpackIterableAggregate($this->cachingIterator))->getIterator();
}

public function hasNext(): bool
{
return $this->cachingIterator->hasNext();
}
}
5 changes: 5 additions & 0 deletions src/SimpleCachingIteratorAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public function getIterator(): Generator
yield $this->iterator->key() => $this->iterator->current();
}
}

public function hasNext(): bool
{
return $this->iterator->hasNext();
}
}
14 changes: 14 additions & 0 deletions tests/unit/CachingIteratorAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace tests\loophp\iterators;

use ArrayIterator;
use Generator;
use loophp\iterators\CachingIteratorAggregate;
use PHPUnit\Framework\TestCase;
Expand All @@ -20,6 +21,19 @@
*/
final class CachingIteratorAggregateTest extends TestCase
{
public function testHasNext(): void
{
$range = range('a', 'c');
$iteratorAggregate = new CachingIteratorAggregate(new ArrayIterator($range));
$iterator = $iteratorAggregate->getIterator();

self::assertTrue($iteratorAggregate->hasNext());
$iterator->next();
self::assertTrue($iteratorAggregate->hasNext());
$iterator->next();
self::assertFalse($iteratorAggregate->hasNext());
}

public function testWithAGenerator(): void
{
$input = static function (): Generator {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/SimpleCachingIteratorAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace tests\loophp\iterators;

use ArrayIterator;
use Generator;
use loophp\iterators\SimpleCachingIteratorAggregate;
use PHPUnit\Framework\TestCase;
Expand All @@ -19,6 +20,19 @@
*/
final class SimpleCachingIteratorAggregateTest extends TestCase
{
public function testHasNext(): void
{
$range = range('a', 'c');
$iteratorAggregate = new SimpleCachingIteratorAggregate(new ArrayIterator($range));
$iterator = $iteratorAggregate->getIterator();

self::assertTrue($iteratorAggregate->hasNext());
$iterator->next();
self::assertTrue($iteratorAggregate->hasNext());
$iterator->next();
self::assertFalse($iteratorAggregate->hasNext());
}

public function testWithAGenerator(): void
{
$input = static function () use (&$stack): Generator {
Expand Down

0 comments on commit 0d80e09

Please sign in to comment.