* Overloads the relevant methods of the real ResultsWrapper so it
* doesn't go anywhere near an actual database.
*/
-class FakeResultWrapper extends ResultWrapper {
- /** @var stdClass[] $result */
+class FakeResultWrapper implements IResultWrapper {
+ /** @var stdClass[]|array[] */
+ protected $result;
+
+ /** @var int */
+ protected $pos = 0;
/**
- * @param stdClass[] $rows
+ * @param stdClass[]|array[]|FakeResultWrapper $result
*/
- function __construct( array $rows ) {
- parent::__construct( null, $rows );
+ public function __construct( $result ) {
+ if ( $result instanceof self ) {
+ $this->result = $result->result;
+ } else {
+ $this->result = $result;
+ }
}
- function numRows() {
+ public function numRows() {
return count( $this->result );
}
- function fetchRow() {
- if ( $this->pos < count( $this->result ) ) {
- $this->currentRow = $this->result[$this->pos];
- } else {
- $this->currentRow = false;
- }
- $this->pos++;
- if ( is_object( $this->currentRow ) ) {
- return get_object_vars( $this->currentRow );
- } else {
- return $this->currentRow;
- }
+ public function fetchObject() {
+ $current = $this->current();
+
+ $this->next();
+
+ return $current;
}
- function seek( $row ) {
- $this->pos = $row;
+ public function fetchRow() {
+ $row = $this->valid() ? $this->result[$this->pos] : false;
+
+ $this->next();
+
+ return is_object( $row ) ? get_object_vars( $row ) : $row;
}
- function free() {
+ public function seek( $pos ) {
+ $this->pos = $pos;
}
- function fetchObject() {
- $this->fetchRow();
- if ( $this->currentRow ) {
- return (object)$this->currentRow;
- } else {
- return false;
- }
+ public function free() {
+ $this->result = null;
}
- function rewind() {
+ public function rewind() {
$this->pos = 0;
- $this->currentRow = null;
}
- function next() {
- return $this->fetchObject();
+ public function current() {
+ $row = $this->valid() ? $this->result[$this->pos] : false;
+
+ return is_array( $row ) ? (object)$row : $row;
+ }
+
+ public function key() {
+ return $this->pos;
+ }
+
+ public function next() {
+ $this->pos++;
+
+ return $this->current();
+ }
+
+ public function valid() {
+ return array_key_exists( $this->pos, $this->result );
}
}