-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChunkedIterator.php
48 lines (40 loc) · 1.28 KB
/
ChunkedIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Eloquent\Iterators;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Override;
use function count;
/**
* The iterator that grabs rows by chunk.
*
* Similar to {@link \Illuminate\Database\Query\Builder::chunk()} but uses
* generators instead of {@link \Closure}. Be careful, you should not modify/delete
* the items while iteration or you will get unexpected results (eg missing
* items). If you need to modify/delete items while iteration you can use
* {@link ChunkedChangeSafeIterator}.
*
* @see ChunkedChangeSafeIterator
*
* @template TItem of Model
*
* @extends IteratorImpl<TItem>
*/
class ChunkedIterator extends IteratorImpl {
#[Override]
protected function getChunk(Builder $builder, int $chunk): Collection {
$builder
->offset($this->getOffset())
->limit($chunk);
return $builder->get();
}
#[Override]
protected function chunkProcessed(Collection $items): bool {
$this->setOffset($this->getOffset() + count($items));
return parent::chunkProcessed($items);
}
#[Override]
public function getOffset(): int {
return (int) parent::getOffset();
}
}