ChunkingIterator::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace itertools;
4
5
use IteratorIterator;
6
use Traversable;
7
use ArrayIterator;
8
9
10
/**
11
 * splits a iterator into smaller chunks. this can be used for batch processing.
12
 *
13
 * Example:
14
 *     $iterator = new rangeiterator();
15
 *     $batchsize = 100;
16
 *     foreach(new hunkingiterator($iterator, $batchsize) as $chunk) {
17
 *         $pdo->starttransaction();
18
 *         foreach($chunk as $element) {
19
 *             // process the iterator elements. using the transaction inside the chunkiterator makes sure the transaction stays small
20
 *         }
21
 *         $pdo->commit();
22
 *     }
23
 */
24
class ChunkingIterator extends IteratorIterator
25
{
26
    protected $chunkSize;
27
    protected $chunk;
28
    protected $chunkIndex;
29
30
    public function __construct($iterator, $chunkSize)
31
    {
32
        parent::__construct(is_array($iterator) ? new ArrayIterator($iterator) : $iterator);
33
        $this->chunkSize = $chunkSize;
34
        $this->chunk = array();
35
    }
36
37
    public function rewind()
38
    {
39
        $this->getInnerIterator()->rewind();
40
        $this->chunk = array();
41
        $this->chunkIndex = 0;
42
    }
43
44
    protected function ensureBatchPresent()
45
    {
46
        if(!is_null(key($this->chunk))) {
47
            // chunk not completely fetched;
48
            return;
49
        }
50
        $inner = $this->getInnerIterator();
51
        for($i = 0; $i < $this->chunkSize && $inner->valid(); $i++) {
52
            $this->chunk[] = $inner->current();
53
            $inner->next();
54
        }
55
    }
56
57
    public function key()
58
    {
59
        return $this->chunkIndex;
60
    }
61
62
    public function next()
63
    {
64
        $this->chunk = array();
65
        $this->chunkIndex += 1;
66
    }
67
68
    public function current()
69
    {
70
        $this->ensureBatchPresent();
71
        return $this->chunk;
72
    }
73
74
    public function valid()
75
    {
76
        $this->ensureBatchPresent();
77
        return count($this->chunk) != 0;
78
    }
79
}
80
81