00001 <?php
00002
00016
00017
00018
00019 import('core.ItemIterator');
00020
00021 class DAOResultFactory extends ItemIterator {
00023 var $dao;
00024
00026 var $functionName;
00027
00029 var $records;
00030
00032 var $wasEmpty;
00033
00034 var $isFirst;
00035 var $isLast;
00036 var $page;
00037 var $count;
00038 var $pageCount;
00039
00047 function DAOResultFactory(&$records, &$dao, $functionName) {
00048 $this->functionName = $functionName;
00049 $this->dao = &$dao;
00050
00051 if (!$records || $records->EOF) {
00052 if ($records) $records->Close();
00053 $this->records = null;
00054 $this->wasEmpty = true;
00055 $this->page = 1;
00056 $this->isFirst = true;
00057 $this->isLast = true;
00058 $this->count = 0;
00059 $this->pageCount = 1;
00060 }
00061 else {
00062 $this->records = &$records;
00063 $this->wasEmpty = false;
00064 $this->page = $records->AbsolutePage();
00065 $this->isFirst = $records->atFirstPage();
00066 $this->isLast = $records->atLastPage();
00067 $this->count = $records->MaxRecordCount();
00068 $this->pageCount = $records->LastPageNo();
00069 }
00070 }
00071
00076 function &next() {
00077 if ($this->records == null) return $this->records;
00078 if (!$this->records->EOF) {
00079 $functionName = &$this->functionName;
00080 $dao = &$this->dao;
00081 $row = &$this->records->getRowAssoc(false);
00082 $result = &$dao->$functionName($row);
00083 if (!$this->records->MoveNext()) $this->_cleanup();
00084 return $result;
00085 } else {
00086 $this->_cleanup();
00087 $nullVar = null;
00088 return $nullVar;
00089 }
00090 }
00091
00095 function &nextWithKey() {
00096
00097
00098 return array(null, $this->next());
00099 }
00100
00101 function atFirstPage() {
00102 return $this->isFirst;
00103 }
00104
00105 function atLastPage() {
00106 return $this->isLast;
00107 }
00108
00109 function getPage() {
00110 return $this->page;
00111 }
00112
00113 function getCount() {
00114 return $this->count;
00115 }
00116
00117 function getPageCount() {
00118 return $this->pageCount;
00119 }
00120
00125 function eof() {
00126 if ($this->records == null) return true;
00127 if ($this->records->EOF) {
00128 $this->_cleanup();
00129 return true;
00130 }
00131 return false;
00132 }
00133
00138 function wasEmpty() {
00139 return $this->wasEmpty;
00140 }
00141
00146 function _cleanup() {
00147 $this->records->close();
00148 unset($this->records);
00149 $this->records = null;
00150 }
00151
00152 function &toArray() {
00153 $returner = array();
00154 while (!$this->eof()) {
00155 $returner[] = $this->next();
00156 }
00157 return $returner;
00158 }
00159
00160 function isInBounds() {
00161 return ($this->pageCount >= $this->page);
00162 }
00163
00164 function &getLastPageRangeInfo() {
00165 import('db.DBResultRange');
00166 $returner =& new DBResultRange($this->count, $this->pageCount);
00167 return $returner;
00168 }
00169 }
00170
00171 ?>