blob: 1edc5748ac78c5816230022c4569b53bdc6523a3 (
plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
<?php
namespace SMW\Iterators;
use ArrayIterator;
use InvalidArgumentException;
use IteratorIterator;
use RuntimeException;
use Traversable;
/**
* @see Guzzle::ChunkedIterator
*
* @license GNU GPL v2+
* @since 3.0
*/
class ChunkedIterator extends IteratorIterator {
/**
* @var integer
*/
private $chunkSize = 0;
/**
* @var array
*/
private $chunk;
/**
* @since 3.0
*
* @param Traversable|array $iterator
* @param integer $chunkSize
*/
public function __construct( $iterable, $chunkSize = 500 ) {
$chunkSize = (int)$chunkSize;
if ( is_array( $iterable ) ) {
$iterable = new ArrayIterator( $iterable );
}
if ( !$iterable instanceof Traversable ) {
throw new RuntimeException( "ChunkedIterator expected an Traversable" );
}
if ( $chunkSize < 0 ) {
throw new InvalidArgumentException( "$chunkSize is lower than 0" );
}
parent::__construct( $iterable );
$this->chunkSize = $chunkSize;
}
/**
* @since 3.0
*
* {@inheritDoc}
*/
public function rewind() {
parent::rewind();
$this->next();
}
/**
* @since 3.0
*
* {@inheritDoc}
*/
public function next() {
$this->chunk = [];
for ( $i = 0; $i < $this->chunkSize && parent::valid(); $i++ ) {
$this->chunk[] = parent::current();
parent::next();
}
}
/**
* @since 3.0
*
* {@inheritDoc}
*/
public function current() {
return $this->chunk;
}
/**
* @since 3.0
*
* {@inheritDoc}
*/
public function valid() {
return (bool) $this->chunk;
}
}
|