1: <?php
2:
3: namespace Guzzle\Iterator;
4:
5: /**
6: * Pulls out chunks from an inner iterator and yields the chunks as arrays
7: */
8: class ChunkedIterator extends \IteratorIterator
9: {
10: /**
11: * @var int Size of each chunk
12: */
13: protected $chunkSize;
14:
15: /**
16: * @var array Current chunk
17: */
18: protected $chunk;
19:
20: /**
21: * @param \Traversable $iterator Traversable iterator
22: * @param int $chunkSize Size to make each chunk
23: */
24: public function __construct(\Traversable $iterator, $chunkSize)
25: {
26: parent::__construct($iterator);
27: $this->chunkSize = $chunkSize;
28: }
29:
30: /**
31: * {@inheritdoc}
32: */
33: public function rewind()
34: {
35: $this->next();
36: }
37:
38: /**
39: * {@inheritdoc}
40: */
41: public function next()
42: {
43: $this->chunk = array();
44: $inner = $this->getInnerIterator();
45: for ($i = 0; $i < $this->chunkSize && $inner->valid(); $i++) {
46: $this->chunk[] = $inner->current();
47: $inner->next();
48: }
49: }
50:
51: /**
52: * {@inheritdoc}
53: */
54: public function current()
55: {
56: return $this->chunk;
57: }
58:
59: /**
60: * {@inheritdoc}
61: */
62: public function valid()
63: {
64: return !empty($this->chunk);
65: }
66: }
67: