Merge "Reduced some master queries by adding flags to Revision functions."
[lhc/web/wiklou.git] / includes / db / ORMResult.php
1 <?php
2 /**
3 * ORMIterator that takes a ResultWrapper object returned from
4 * a select operation returning IORMRow objects (ie IORMTable::select).
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @since 1.20
22 *
23 * @file ORMResult.php
24 * @ingroup ORM
25 *
26 * @licence GNU GPL v2 or later
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
28 */
29
30 class ORMResult implements ORMIterator {
31
32 /**
33 * @var ResultWrapper
34 */
35 protected $res;
36
37 /**
38 * @var integer
39 */
40 protected $key;
41
42 /**
43 * @var IORMRow
44 */
45 protected $current;
46
47 /**
48 * @var IORMTable
49 */
50 protected $table;
51
52 /**
53 * @param IORMTable $table
54 * @param ResultWrapper $res
55 */
56 public function __construct( IORMTable $table, ResultWrapper $res ) {
57 $this->table = $table;
58 $this->res = $res;
59 $this->key = 0;
60 $this->setCurrent( $this->res->current() );
61 }
62
63 /**
64 * @param $row
65 */
66 protected function setCurrent( $row ) {
67 if ( $row === false ) {
68 $this->current = false;
69 } else {
70 $this->current = $this->table->newFromDBResult( $row );
71 }
72 }
73
74 /**
75 * @return integer
76 */
77 public function count() {
78 return $this->res->numRows();
79 }
80
81 /**
82 * @return boolean
83 */
84 public function isEmpty() {
85 return $this->res->numRows() === 0;
86 }
87
88 /**
89 * @return IORMRow
90 */
91 public function current() {
92 return $this->current;
93 }
94
95 /**
96 * @return integer
97 */
98 public function key() {
99 return $this->key;
100 }
101
102 public function next() {
103 $row = $this->res->next();
104 $this->setCurrent( $row );
105 $this->key++;
106 }
107
108 public function rewind() {
109 $this->res->rewind();
110 $this->key = 0;
111 $this->setCurrent( $this->res->current() );
112 }
113
114 /**
115 * @return boolean
116 */
117 public function valid() {
118 return $this->current !== false;
119 }
120
121 }