_source = array(); if (!is_null($data)) { $this->loadSource($data); } } /** * Get the number of elements in the collection * @return integer Count */ public function count() { return count($this->_source); } /** * Reset the iterator to the beginning of the data * @return void */ public function rewind() { $this->_sourceIndex = 0; } /** * Returns the data at the current index in the collection * * @return mixed the current row or null if no rows exist. */ public function current() { if(isset($this->_source[$this->_sourceIndex])) { return $this->_source[$this->_sourceIndex]; } else { return null; } } /** * Returns the collections current index number * @return mixed the current row number (starts at 0), null if there is no data */ public function key() { return $this->_sourceIndex; } /** * @return mixed the next row number collection, or null if not another row. */ public function next() { return ++$this->_sourceIndex; } /** * checks if the iterator is valid * @return boolean is the iterator valid */ public function valid() { return 0 <= $this->_sourceIndex && $this->_sourceIndex < $this->count(); } /** * Allow name value pairs to be added to the ArrayCollection * @param mixed name pair * @param mixed value pair */ public function __set($name, $value) { if($name == 'externalizedData') { $this->loadSource($value); } else { $this->_source[] = array($name => $value); } } /** * Builds an Array into an ArrayCollection and handles Zend_DB_Table * * @param array data to be added to the collection * @todo Should fire an exception if the data is not an array */ private function loadSource($data) { if (is_array($data)) { foreach($data as $row) { if ($row instanceof Zend_Db_Table_Row_Abstract) { $row = $row->toArray(); } if (is_object($row)) { $this->_source[] = $row; } else if (is_array($row)) { $source_row = array(); foreach($row as $colkey => $colvalue) { $source_row[$colkey] = $colvalue; } if ($source_row) { $this->_source[] = $source_row; } } } } } }