-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
RecursiveIterableAggregate
(#61)
* Add `RecursiveIterableAggregate` * Change `RecursiveIterableAggregate` recursive call to loop * Update `README` to include `RecursiveIterableAggregate`
- Loading branch information
1 parent
a443a53
commit 74f7aa2
Showing
3 changed files
with
213 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
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\iterators; | ||
|
||
use Closure; | ||
use Generator; | ||
use Iterator; | ||
use IteratorAggregate; | ||
use SplStack; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
* | ||
* @implements IteratorAggregate<TKey, T> | ||
*/ | ||
class RecursiveIterableAggregate implements IteratorAggregate | ||
{ | ||
/** | ||
* @param iterable<TKey, T> $iterable | ||
* @param (Closure(T, TKey, iterable<TKey, T>): iterable<TKey, T>) $closure | ||
*/ | ||
public function __construct(private iterable $iterable, private Closure $closure) {} | ||
|
||
/** | ||
* @return Generator<TKey, T> | ||
*/ | ||
public function getIterator(): Generator | ||
{ | ||
/** @var SplStack<Iterator> $iterables */ | ||
$iterables = new SplStack(); | ||
$iterables->push(new IterableIterator($this->iterable)); | ||
|
||
while (!$iterables->isEmpty()) { | ||
$currentIterable = $iterables->top(); | ||
|
||
while ($currentIterable->valid()) { | ||
$currentValue = $currentIterable->current(); | ||
$currentKey = $currentIterable->key(); | ||
|
||
yield $currentKey => $currentValue; | ||
|
||
$subIterable = new IterableIterator( | ||
($this->closure)($currentValue, $currentKey, $this->iterable) | ||
); | ||
|
||
$currentIterable->next(); | ||
$subIterable->rewind(); | ||
|
||
if ($subIterable->valid()) { | ||
$iterables->push($subIterable); | ||
|
||
continue 2; | ||
} | ||
} | ||
|
||
$iterables->pop(); | ||
} | ||
} | ||
} |
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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests\loophp\iterators; | ||
|
||
use Generator; | ||
use loophp\iterators\RecursiveIterableAggregate; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversDefaultClass \loophp\iterators | ||
*/ | ||
final class RecursiveIterableAggregateTest extends TestCase | ||
{ | ||
/** | ||
* @return Generator<array{0: array, 1: array}> | ||
*/ | ||
public static function provideBasicCases(): iterable | ||
{ | ||
yield [ | ||
[ | ||
'i0' => [ | ||
'index' => 0, | ||
'children' => [], | ||
], | ||
'i1' => [ | ||
'index' => 1, | ||
'children' => [], | ||
], | ||
], | ||
[ | ||
'i0' => [ | ||
'index' => 0, | ||
'children' => [], | ||
], | ||
'i1' => [ | ||
'index' => 1, | ||
'children' => [], | ||
], | ||
], | ||
]; | ||
|
||
yield [ | ||
[ | ||
'i0' => [ | ||
'index' => 0, | ||
'children' => [ | ||
'i1' => [ | ||
'index' => 1, | ||
'children' => [ | ||
'i2' => [ | ||
'index' => 2, | ||
'children' => [], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
[ | ||
'i0' => [ | ||
'index' => 0, | ||
'children' => [ | ||
'i1' => [ | ||
'index' => 1, | ||
'children' => [ | ||
'i2' => [ | ||
'index' => 2, | ||
'children' => [], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
'i1' => [ | ||
'index' => 1, | ||
'children' => [ | ||
'i2' => [ | ||
'index' => 2, | ||
'children' => [], | ||
], | ||
], | ||
], | ||
'i2' => [ | ||
'index' => 2, | ||
'children' => [], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideBasicCases | ||
*/ | ||
public function testBasic(array $input, array $expected): void | ||
{ | ||
self::assertEquals( | ||
iterator_to_array( | ||
new RecursiveIterableAggregate($input, static fn (array $i) => $i['children']) | ||
), | ||
$expected | ||
); | ||
} | ||
} |