* @package ISSO
*
*/
-abstract class BSDbResult
+abstract class BSDbResult implements Iterator
{
/**
* The result resource
*/
protected $result;
+ /**
+ * All the results (as used by the iterator implementation)
+ * @var string
+ */
+ protected $iterator;
+
/**
* Sets the resource and returns a result object
*
$this->result = $result;
}
+ /**
+ * Method to call on the result
+ *
+ * @param string Method name
+ *
+ * @return array Array of results
+ */
+ public function fetchAll($method = 'fetchArray')
+ {
+ $results = array();
+ while ($result = $this->$method())
+ {
+ $results[] = $result;
+ }
+ return $results;
+ }
+
/**
* Fetch the query result as an array
*
* @return integer Number of rows
*/
protected abstract function _numRows($result);
+
+ // ###################################################################
+ // # Iterator implementation
+ // ###################################################################
+ /**
+ * Rewinds the iterator by resetting the result pointer
+ *
+ * @return void
+ */
+ public function rewind()
+ {
+ if ($this->iterator == null)
+ {
+ $this->iterator = $this->fetchAll();
+ }
+ reset($this->iterator);
+ }
+
+ /**
+ * Returns the current row
+ *
+ * @return array
+ */
+ public function current()
+ {
+ return current($this->iterator);
+ }
+
+ /**
+ * Returns the key (result pointer index)
+ *
+ * @return integer
+ */
+ public function key()
+ {
+ return key($this->iterator);
+ }
+
+ /**
+ * Advances the iterator and returns the row
+ *
+ * @return array
+ */
+ public function next()
+ {
+ return next($this->iterator);
+ }
+
+ /**
+ * Checks whether the iterator is valid
+ *
+ * @return boolean
+ */
+ public function valid()
+ {
+ return ($this->current() !== false);
+ }
}
/**