UserArrayFromResult.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Class to walk into a list of User objects.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use Wikimedia\Rdbms\IResultWrapper;
  23. class UserArrayFromResult extends UserArray implements Countable {
  24. /** @var IResultWrapper */
  25. public $res;
  26. /** @var int */
  27. public $key;
  28. /** @var bool|User */
  29. public $current;
  30. /**
  31. * @param IResultWrapper $res
  32. */
  33. function __construct( $res ) {
  34. $this->res = $res;
  35. $this->key = 0;
  36. $this->setCurrent( $this->res->current() );
  37. }
  38. /**
  39. * @param bool|stdClass $row
  40. * @return void
  41. */
  42. protected function setCurrent( $row ) {
  43. if ( $row === false ) {
  44. $this->current = false;
  45. } else {
  46. $this->current = User::newFromRow( $row );
  47. }
  48. }
  49. /**
  50. * @return int
  51. */
  52. public function count() {
  53. return $this->res->numRows();
  54. }
  55. /**
  56. * @return User
  57. */
  58. function current() {
  59. return $this->current;
  60. }
  61. function key() {
  62. return $this->key;
  63. }
  64. function next() {
  65. $row = $this->res->next();
  66. $this->setCurrent( $row );
  67. $this->key++;
  68. }
  69. function rewind() {
  70. $this->res->rewind();
  71. $this->key = 0;
  72. $this->setCurrent( $this->res->current() );
  73. }
  74. /**
  75. * @return bool
  76. */
  77. function valid() {
  78. return $this->current !== false;
  79. }
  80. }