MediaWiki  master
FakeResultWrapper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use stdClass;
6 
13  protected $result;
14 
16  protected $pos = 0;
17 
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  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable valid() checks for result not null
43  $row = $this->valid() ? $this->result[$this->pos] : false;
44 
45  $this->next();
46 
47  return is_object( $row ) ? get_object_vars( $row ) : $row;
48  }
49 
50  public function seek( $pos ) {
51  $this->pos = $pos;
52  }
53 
54  public function free() {
55  $this->result = null;
56  }
57 
58  public function rewind() {
59  $this->pos = 0;
60  }
61 
62  public function current() {
63  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable valid() checks for result not null
64  $row = $this->valid() ? $this->result[$this->pos] : false;
65 
66  return is_array( $row ) ? (object)$row : $row;
67  }
68 
69  public function key() {
70  return $this->pos;
71  }
72 
73  public function next() {
74  $this->pos++;
75 
76  return $this->current();
77  }
78 
79  public function valid() {
80  return array_key_exists( $this->pos, $this->result );
81  }
82 }
83 
87 class_alias( FakeResultWrapper::class, 'FakeResultWrapper' );
Wikimedia\Rdbms\FakeResultWrapper\__construct
__construct( $result)
Definition: FakeResultWrapper.php:21
Wikimedia\Rdbms\FakeResultWrapper\current
current()
Definition: FakeResultWrapper.php:62
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\FakeResultWrapper\rewind
rewind()
Definition: FakeResultWrapper.php:58
Wikimedia\Rdbms\FakeResultWrapper
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Definition: FakeResultWrapper.php:11
Wikimedia\Rdbms\FakeResultWrapper\$result
stdClass[] array[] $result
Definition: FakeResultWrapper.php:13
Wikimedia\Rdbms\FakeResultWrapper\free
free()
Free a result object.
Definition: FakeResultWrapper.php:54
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
Wikimedia\Rdbms\FakeResultWrapper\seek
seek( $pos)
Change the position of the cursor in a result object.
Definition: FakeResultWrapper.php:50
Wikimedia\Rdbms\FakeResultWrapper\$pos
int $pos
Definition: FakeResultWrapper.php:16
Wikimedia\Rdbms\FakeResultWrapper\fetchRow
fetchRow()
Fetch the next row from the given result object, in associative array form.
Definition: FakeResultWrapper.php:41
Wikimedia\Rdbms\FakeResultWrapper\numRows
numRows()
Get the number of rows in a result object.
Definition: FakeResultWrapper.php:29
Wikimedia\Rdbms\FakeResultWrapper\key
key()
Definition: FakeResultWrapper.php:69
Wikimedia\Rdbms\FakeResultWrapper\valid
valid()
Definition: FakeResultWrapper.php:79
Wikimedia\Rdbms\FakeResultWrapper\fetchObject
fetchObject()
Fetch the next row from the given result object, in object form.
Definition: FakeResultWrapper.php:33
Wikimedia\Rdbms\FakeResultWrapper\next
next()
Definition: FakeResultWrapper.php:73