Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / resultwrapper / FakeResultWrapper.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use stdClass;
6
7 /**
8 * Overloads the relevant methods of the real ResultsWrapper so it
9 * doesn't go anywhere near an actual database.
10 */
11 class FakeResultWrapper implements IResultWrapper {
12 /** @var stdClass[]|array[] */
13 protected $result;
14
15 /** @var int */
16 protected $pos = 0;
17
18 /**
19 * @param stdClass[]|array[]|FakeResultWrapper $result
20 */
21 public function __construct( $result ) {
22 if ( $result instanceof self ) {
23 $this->result = $result->result;
24 } else {
25 $this->result = $result;
26 }
27 }
28
29 public function numRows() {
30 return count( $this->result );
31 }
32
33 public function fetchObject() {
34 $current = $this->current();
35
36 $this->next();
37
38 return $current;
39 }
40
41 public function fetchRow() {
42 $row = $this->valid() ? $this->result[$this->pos] : false;
43
44 $this->next();
45
46 return is_object( $row ) ? get_object_vars( $row ) : $row;
47 }
48
49 public function seek( $pos ) {
50 $this->pos = $pos;
51 }
52
53 public function free() {
54 $this->result = null;
55 }
56
57 public function rewind() {
58 $this->pos = 0;
59 }
60
61 public function current() {
62 $row = $this->valid() ? $this->result[$this->pos] : false;
63
64 return is_array( $row ) ? (object)$row : $row;
65 }
66
67 public function key() {
68 return $this->pos;
69 }
70
71 public function next() {
72 $this->pos++;
73
74 return $this->current();
75 }
76
77 public function valid() {
78 return array_key_exists( $this->pos, $this->result );
79 }
80 }
81
82 /**
83 * @deprecated since 1.29
84 */
85 class_alias( FakeResultWrapper::class, 'FakeResultWrapper' );