-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
177 additions
and
2 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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\IterableFunctions; | ||
|
||
use Iterator; | ||
use IteratorIterator; | ||
use Traversable; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @template TKey | ||
* @template TValue | ||
* | ||
* @implements Iterator<int, array<TKey, TValue>> | ||
*/ | ||
final class ChunkIterator implements Iterator | ||
{ | ||
/** @var Iterator<TKey, TValue> */ | ||
private Iterator $iterator; | ||
|
||
private int $chunkSize; | ||
|
||
private bool $preserveKeys; | ||
|
||
private int $chunkIndex = 0; | ||
|
||
/** @var array<TKey, TValue> */ | ||
private array $buffer = []; | ||
|
||
/** | ||
* @param Traversable<TKey, TValue> $iterator | ||
*/ | ||
public function __construct( | ||
Traversable $iterator, | ||
int $chunkSize, | ||
bool $preserveKeys = false, | ||
) { | ||
$this->iterator = $iterator instanceof Iterator ? $iterator : new IteratorIterator($iterator); | ||
$this->chunkSize = $chunkSize; | ||
$this->preserveKeys = $preserveKeys; | ||
} | ||
|
||
public function current(): mixed | ||
{ | ||
return $this->buffer; | ||
} | ||
|
||
public function next(): void | ||
{ | ||
$this->fill(); | ||
$this->chunkIndex++; | ||
} | ||
|
||
public function key(): int | ||
{ | ||
return $this->chunkIndex; | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
if ($this->chunkIndex === 0) { | ||
$this->fill(); | ||
} | ||
|
||
return $this->buffer !== []; | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->iterator->rewind(); | ||
$this->chunkIndex = 0; | ||
$this->buffer = []; | ||
} | ||
|
||
private function fill(): void | ||
{ | ||
$this->buffer = []; | ||
$i = 0; | ||
while ($this->iterator->valid() && $i++ < $this->chunkSize) { | ||
$current = $this->iterator->current(); | ||
|
||
if ($this->preserveKeys) { | ||
$this->buffer[$this->iterator->key()] = $current; | ||
} else { | ||
$this->buffer[] = $current; | ||
} | ||
|
||
$this->iterator->next(); | ||
} | ||
} | ||
} |
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
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\IterableFunctions\Tests; | ||
|
||
use function BenTools\IterableFunctions\iterable_chunk; | ||
use function expect; | ||
use function it; | ||
|
||
it('chunks an iterable', function (iterable $fruits): void { | ||
$chunks = iterable_chunk($fruits, 2); | ||
$expectedChunks = [ | ||
['banana', 'apple'], | ||
['strawberry', 'raspberry'], | ||
['pineapple'], | ||
]; | ||
expect([...$chunks])->toEqual($expectedChunks); | ||
})->with(function () { | ||
$fruits = [ | ||
'banana', | ||
'apple', | ||
'strawberry', | ||
'raspberry', | ||
'pineapple', | ||
]; | ||
yield 'array' => [$fruits]; | ||
yield 'traversable' => [(fn () => yield from $fruits)()]; | ||
}); | ||
|
||
it('preserves keys', function (iterable $fruits): void { | ||
$chunks = iterable_chunk($fruits, 2, true); | ||
$expectedChunks = [ | ||
[ | ||
'banana' => 0, | ||
'apple' => 1, | ||
], | ||
[ | ||
'strawberry' => 2, | ||
'raspberry' => 3, | ||
], | ||
['pineapple' => 4], | ||
]; | ||
expect([...$chunks])->toEqual($expectedChunks); | ||
})->with(function () { | ||
$fruits = [ | ||
'banana' => 0, | ||
'apple' => 1, | ||
'strawberry' => 2, | ||
'raspberry' => 3, | ||
'pineapple' => 4, | ||
]; | ||
yield 'array' => [$fruits]; | ||
yield 'traversable' => [(fn () => yield from $fruits)()]; | ||
}); |