bed88092de8435ec1d17303029bc747893aec811
[lhc/web/wiklou.git] / includes / db / ORMResult.php
1 <?php
2
3 /**
4 * Result of a ORMTable::select, which returns ORMRow objects.
5 *
6 * @since 1.20
7 *
8 * @file ORMResult.php
9 *
10 * @licence GNU GPL v2 or later
11 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
12 */
13 class ORMResult implements Iterator {
14
15 /**
16 * @var ResultWrapper
17 */
18 protected $res;
19
20 /**
21 * @var integer
22 */
23 protected $key;
24
25 /**
26 * @var ORMRow
27 */
28 protected $current;
29
30 /**
31 * @var ORMTable
32 */
33 protected $table;
34
35 /**
36 * @param ORMTable $table
37 * @param ResultWrapper $res
38 */
39 public function __construct( ORMTable $table, ResultWrapper $res ) {
40 $this->table = $table;
41 $this->res = $res;
42 $this->key = 0;
43 $this->setCurrent( $this->res->current() );
44 }
45
46 /**
47 * @param $row
48 */
49 protected function setCurrent( $row ) {
50 if ( $row === false ) {
51 $this->current = false;
52 } else {
53 $this->current = $this->table->newFromDBResult( $row );
54 }
55 }
56
57 /**
58 * @return integer
59 */
60 public function count() {
61 return $this->res->numRows();
62 }
63
64 /**
65 * @return boolean
66 */
67 public function isEmpty() {
68 return $this->res->numRows() === 0;
69 }
70
71 /**
72 * @return ORMRow
73 */
74 public function current() {
75 return $this->current;
76 }
77
78 /**
79 * @return integer
80 */
81 public function key() {
82 return $this->key;
83 }
84
85 public function next() {
86 $row = $this->res->next();
87 $this->setCurrent( $row );
88 $this->key++;
89 }
90
91 public function rewind() {
92 $this->res->rewind();
93 $this->key = 0;
94 $this->setCurrent( $this->res->current() );
95 }
96
97 /**
98 * @return boolean
99 */
100 public function valid() {
101 return $this->current !== false;
102 }
103
104 }