Remove ugly IP code duplication
[lhc/web/wiklou.git] / includes / UserArray.php
1 <?php
2
3 abstract class UserArray implements Iterator {
4 static function newFromResult( $res ) {
5 $userArray = null;
6 if ( !wfRunHooks( 'UserArrayFromResult', array( &$userArray, $res ) ) ) {
7 return null;
8 }
9 if ( $userArray === null ) {
10 $userArray = self::newFromResult_internal( $res );
11 }
12 return $userArray;
13 }
14
15 protected static function newFromResult_internal( $res ) {
16 $userArray = new UserArrayFromResult( $res );
17 return $userArray;
18 }
19 }
20
21 class UserArrayFromResult extends UserArray {
22 var $res;
23 var $key, $current;
24
25 function __construct( $res ) {
26 $this->res = $res;
27 $this->key = 0;
28 $this->setCurrent( $this->res->current() );
29 }
30
31 protected function setCurrent( $row ) {
32 if ( $row === false ) {
33 $this->current = false;
34 } else {
35 $this->current = User::newFromRow( $row );
36 }
37 }
38
39 function current() {
40 return $this->current;
41 }
42
43 function key() {
44 return $this->key;
45 }
46
47 function next() {
48 $row = $this->res->next();
49 $this->setCurrent( $row );
50 $this->key++;
51 }
52
53 function rewind() {
54 $this->res->rewind();
55 $this->key = 0;
56 $this->setCurrent( $this->res->current() );
57 }
58
59 function valid() {
60 return $this->current !== false;
61 }
62 }