Skip to content

Commit

Permalink
Improve Query Limit implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 20, 2024
1 parent 81b7b58 commit 8708a75
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Query/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use ArrayIterator;
use Iterator;
use IteratorIterator;
use IteratorAggregate;
use LimitIterator;
use Traversable;

Expand Down Expand Up @@ -44,14 +44,22 @@ public static function new(int $offset, int $length): self
*/
public function slice(iterable $value): LimitIterator
{
if ($value instanceof IteratorAggregate) {
$value = $value->getIterator();
}

return new LimitIterator(
match (true) {
$value instanceof Iterator => $value,
$value instanceof Traversable => new IteratorIterator($value),
$value instanceof Traversable => (function () use ($value): Iterator {
foreach ($value as $offset => $record) {
yield $offset => $record;
}
})(),
default => new ArrayIterator($value),
},
$this->offset,
$this->length,
$this->length
);
}
}

0 comments on commit 8708a75

Please sign in to comment.