-
-
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
InterruptableIterableAggregate
.
- Loading branch information
Showing
4 changed files
with
150 additions
and
5 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<files psalm-version="v4.15.0@a1b5e489e6fcebe40cb804793d964e99fc347820"> | ||
<file src="src/RandomIterableAggregate.php"> | ||
<InvalidPropertyAssignmentValue occurrences="1"> | ||
<code>new PackIterableAggregate($iterable)</code> | ||
</InvalidPropertyAssignmentValue> | ||
<files psalm-version="4.23.0@f1fe6ff483bf325c803df9f510d09a03fd796f88"> | ||
<file src="src/InterruptableIteratorAggregate.php"> | ||
<ImplementedReturnTypeMismatch occurrences="1"> | ||
<code>Generator<Generator<TKey, T>, array{0: TKey, 1: T}></code> | ||
</ImplementedReturnTypeMismatch> | ||
</file> | ||
</files> |
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,64 @@ | ||
<?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; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
* | ||
* @implements IteratorAggregate<TKey, T> | ||
*/ | ||
final class InterruptableIteratorAggregate implements IteratorAggregate | ||
{ | ||
public const BREAK = 'break'; | ||
|
||
/** | ||
* @var iterable<TKey, T> | ||
*/ | ||
private iterable $iterable; | ||
|
||
/** | ||
* @param iterable<TKey, T> $iterable | ||
*/ | ||
public function __construct(iterable $iterable) | ||
{ | ||
$this->iterable = $iterable; | ||
} | ||
|
||
/** | ||
* @return Generator<Generator<TKey, T>, array{0: TKey, 1: T}> | ||
*/ | ||
public function getIterator(): Generator | ||
{ | ||
$generator = $this->getGenerator(); | ||
|
||
foreach ($generator as $key => $value) { | ||
yield $generator => [$key, $value]; | ||
} | ||
} | ||
|
||
/** | ||
* @return Generator<TKey, T> | ||
*/ | ||
private function getGenerator(): Generator | ||
{ | ||
foreach ($this->iterable as $key => $value) { | ||
/** @var mixed $return */ | ||
$return = yield $key => $value; | ||
|
||
if (self::BREAK === $return) { | ||
break; | ||
} | ||
} | ||
} | ||
} |
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,51 @@ | ||
<?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 Generator; | ||
use IteratorAggregate; | ||
use loophp\iterators\InterruptableIteratorAggregate; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @coversDefaultClass \loophp\iterators | ||
*/ | ||
final class InterruptableIteratorAggregateTest extends TestCase | ||
{ | ||
public function testBreakInterruption() | ||
{ | ||
$naturals = static function () { | ||
$i = 0; | ||
|
||
while (true) { | ||
yield $i++; | ||
} | ||
}; | ||
|
||
$subject = new InterruptableIteratorAggregate($naturals()); | ||
|
||
self::assertSame( | ||
range(0, 10), | ||
iterator_to_array($this->getSubjectGenerator($subject)) | ||
); | ||
} | ||
|
||
private function getSubjectGenerator(IteratorAggregate $subject): Generator | ||
{ | ||
foreach ($subject as $generator => [$key, $value]) { | ||
yield $key => $value; | ||
|
||
if (10 === $value) { | ||
$generator->send(InterruptableIteratorAggregate::BREAK); | ||
} | ||
} | ||
} | ||
} |