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