Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Applies the same callback to all recorset records.
  19   *
  20   * @since      Moodle 2.9
  21   * @package    core
  22   * @category   dml
  23   * @copyright  2015 David Monllao
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace core\dml;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Iterator that walks through a moodle_recordset applying the provided function.
  33   *
  34   * The internal recordset can be closed using the close() function.
  35   *
  36   * Note that consumers of this class are responsible of closing the recordset,
  37   * although there are some implicit closes under some ciscumstances:
  38   * - Once all recordset records have been iterated
  39   * - The object is destroyed
  40   *
  41   * @since      Moodle 2.9
  42   * @package    core
  43   * @category   dml
  44   * @copyright  2015 David Monllao
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class recordset_walk implements \Iterator {
  48  
  49      /**
  50       * @var \moodle_recordset The recordset.
  51       */
  52      protected $recordset;
  53  
  54      /**
  55       * @var callable The callback.
  56       */
  57      protected $callback;
  58  
  59      /**
  60       * @var mixed|null Extra param for the callback.
  61       */
  62      protected $callbackextra;
  63  
  64      /**
  65       * Create a new iterator applying the callback to each record.
  66       *
  67       * @param \moodle_recordset $recordset Recordset to iterate.
  68       * @param callable $callback Apply this function to each record. If using a method, it should be public.
  69       * @param mixed $callbackextra An extra single parameter to pass to the callback. Use a container to pass multiple values.
  70       */
  71      public function __construct(\moodle_recordset $recordset, callable $callback, $callbackextra = null) {
  72          $this->recordset = $recordset;
  73          $this->callback = $callback;
  74          $this->callbackextra = $callbackextra;
  75      }
  76  
  77      /**
  78       * Closes the recordset.
  79       *
  80       * @return void
  81       */
  82      public function __destruct() {
  83          $this->close();
  84      }
  85  
  86      /**
  87       * Returns the current element after applying the callback.
  88       *
  89       * @return mixed|bool The returned value type will depend on the callback.
  90       */
  91      #[\ReturnTypeWillChange]
  92      public function current() {
  93  
  94          if (!$this->recordset->valid()) {
  95              return false;
  96          }
  97  
  98          if (!$record = $this->recordset->current()) {
  99              return false;
 100          }
 101  
 102          // Apply callback and return.
 103          if (!is_null($this->callbackextra)) {
 104              return call_user_func($this->callback, $record, $this->callbackextra);
 105          } else {
 106              return call_user_func($this->callback, $record);
 107          }
 108      }
 109  
 110      /**
 111       * Moves the internal pointer to the next record.
 112       *
 113       * @return void
 114       */
 115      public function next(): void {
 116          $this->recordset->next();
 117      }
 118  
 119      /**
 120       * Returns current record key.
 121       *
 122       * @return int
 123       */
 124      #[\ReturnTypeWillChange]
 125      public function key() {
 126          return $this->recordset->key();
 127      }
 128  
 129      /**
 130       * Returns whether the current position is valid or not.
 131       *
 132       * If we reached the end of the recordset we close as we
 133       * don't allow rewinds. Doing do so we reduce the chance
 134       * of unclosed recordsets.
 135       *
 136       * @return bool
 137       */
 138      public function valid(): bool {
 139          if (!$valid = $this->recordset->valid()) {
 140              $this->close();
 141          }
 142          return $valid;
 143      }
 144  
 145      /**
 146       * Rewind is not supported.
 147       *
 148       * @return void
 149       */
 150      public function rewind(): void {
 151          // No rewind as it is not implemented in moodle_recordset.
 152          return;
 153      }
 154  
 155      /**
 156       * Closes the recordset.
 157       *
 158       * @return void
 159       */
 160      public function close() {
 161          $this->recordset->close();
 162      }
 163  }