-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to iterate over results with foreach($result AS $row…
…No => $rowContent) ...
- Loading branch information
1 parent
50e6f79
commit 939cb81
Showing
4 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace Cassandra\Response; | ||
|
||
/** | ||
* @author Dennis Birkholz <[email protected]> | ||
*/ | ||
class ResultIterator implements \Iterator { | ||
/** | ||
* Stream containing the raw result data | ||
* | ||
* @var StreamReader | ||
*/ | ||
private $stream; | ||
|
||
/** | ||
* Offset to start reading data in this stream | ||
* | ||
* @var int | ||
*/ | ||
private $offset; | ||
|
||
/** | ||
* Metadata generated by the creating Result | ||
* | ||
* @var array | ||
*/ | ||
private $metadata; | ||
|
||
/** | ||
* Class to use for each row of data | ||
* | ||
* @var string | ||
*/ | ||
private $rowClass; | ||
|
||
/** | ||
* Number of available rows in the resultset | ||
* | ||
* @var int | ||
*/ | ||
private $count; | ||
|
||
/** | ||
* Current row | ||
* | ||
* @var int | ||
*/ | ||
private $row = 0; | ||
|
||
|
||
public function __construct(StreamReader $stream, array $metadata, $rowClass) { | ||
$this->stream = clone $stream; | ||
$this->metadata = $metadata; | ||
$this->count = $this->stream->readInt(); | ||
$this->offset = $this->stream->pos(); | ||
$this->rewind(); | ||
} | ||
|
||
public function current() { | ||
$data = []; | ||
|
||
foreach ($this->metadata['columns'] as $column) { | ||
$data[$column['name']] = $this->stream->readValue($column['type']); | ||
} | ||
|
||
$rowClass = $this->rowClass; | ||
return ($rowClass === null ? $data : new $rowClass($data)); | ||
} | ||
|
||
/** | ||
* The current position in this result set | ||
* | ||
* @return int | ||
*/ | ||
public function key() { | ||
return $this->row; | ||
} | ||
|
||
/** | ||
* Move forward to next element | ||
* | ||
* @return void | ||
*/ | ||
public function next() { | ||
$this->row++; | ||
} | ||
|
||
/** | ||
* Reset the result set | ||
* | ||
* @return void | ||
*/ | ||
public function rewind() { | ||
$this->row = 0; | ||
$this->stream->offset($this->offset); | ||
} | ||
|
||
/** | ||
* Checks if current position is valid | ||
* | ||
* @return boolean | ||
*/ | ||
public function valid() { | ||
return (($this->row >= 0) && ($this->row < $this->count)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters