Skip to content

Commit 88b5e4f

Browse files
Make it possible to iterate over results with foreach($result AS $rowNo => $rowContent) ...
1 parent 323613d commit 88b5e4f

4 files changed

Lines changed: 126 additions & 2 deletions

File tree

src/Response/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Response implements Frame{
99
protected $_header;
1010

1111
/**
12-
* @var
12+
* @var StreamReader
1313
*/
1414
protected $_stream;
1515

src/Response/Result.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Cassandra\Protocol\Frame;
44
use Cassandra\Type;
55

6-
class Result extends Response {
6+
class Result extends Response implements \IteratorAggregate {
77
const VOID = 0x0001;
88
const ROWS = 0x0002;
99
const SET_KEYSPACE = 0x0003;
@@ -311,4 +311,12 @@ public function fetchOne(){
311311

312312
return null;
313313
}
314+
315+
/**
316+
* @return
317+
*/
318+
public function getIterator() {
319+
return new ResultIterator($this->_stream, $this->getMetadata(), $this->_rowClass);
320+
}
321+
314322
}

src/Response/ResultIterator.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Cassandra\Response;
4+
5+
/**
6+
* @author Dennis Birkholz <birkholz@pubgrade.com>
7+
*/
8+
class ResultIterator implements \Iterator {
9+
/**
10+
* Stream containing the raw result data
11+
*
12+
* @var StreamReader
13+
*/
14+
private $stream;
15+
16+
/**
17+
* Offset to start reading data in this stream
18+
*
19+
* @var int
20+
*/
21+
private $offset;
22+
23+
/**
24+
* Metadata generated by the creating Result
25+
*
26+
* @var array
27+
*/
28+
private $metadata;
29+
30+
/**
31+
* Class to use for each row of data
32+
*
33+
* @var string
34+
*/
35+
private $rowClass;
36+
37+
/**
38+
* Number of available rows in the resultset
39+
*
40+
* @var int
41+
*/
42+
private $count;
43+
44+
/**
45+
* Current row
46+
*
47+
* @var int
48+
*/
49+
private $row = 0;
50+
51+
52+
public function __construct(StreamReader $stream, array $metadata, $rowClass) {
53+
$this->stream = clone $stream;
54+
$this->metadata = $metadata;
55+
$this->count = $this->stream->readInt();
56+
$this->offset = $this->stream->pos();
57+
$this->rewind();
58+
}
59+
60+
public function current() {
61+
$data = [];
62+
63+
foreach ($this->metadata['columns'] as $column) {
64+
$data[$column['name']] = $this->stream->readValue($column['type']);
65+
}
66+
67+
$rowClass = $this->rowClass;
68+
return ($rowClass === null ? $data : new $rowClass($data));
69+
}
70+
71+
/**
72+
* The current position in this result set
73+
*
74+
* @return int
75+
*/
76+
public function key() {
77+
return $this->row;
78+
}
79+
80+
/**
81+
* Move forward to next element
82+
*
83+
* @return void
84+
*/
85+
public function next() {
86+
$this->row++;
87+
}
88+
89+
/**
90+
* Reset the result set
91+
*
92+
* @return void
93+
*/
94+
public function rewind() {
95+
$this->row = 0;
96+
$this->stream->offset($this->offset);
97+
}
98+
99+
/**
100+
* Checks if current position is valid
101+
*
102+
* @return boolean
103+
*/
104+
public function valid() {
105+
return (($this->row >= 0) && ($this->row < $this->count));
106+
}
107+
}

src/Response/StreamReader.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function offset($offset){
4040
public function reset(){
4141
$this->offset = 0;
4242
}
43+
44+
/**
45+
* Get the current position of this stream
46+
*
47+
* @return int
48+
*/
49+
public function pos() {
50+
return $this->offset;
51+
}
4352

4453
/**
4554
* Read single character.

0 commit comments

Comments
 (0)