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] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  if (!class_exists('Google_Client')) {
   4    require_once dirname(__FILE__) . '/autoload.php';
   5  }
   6  
   7  /**
   8   * Extension to the regular Google_Model that automatically
   9   * exposes the items array for iteration, so you can just
  10   * iterate over the object rather than a reference inside.
  11   */
  12  class Google_Collection extends Google_Model implements Iterator, Countable
  13  {
  14    protected $collection_key = 'items';
  15  
  16    public function rewind(): void
  17    {
  18      if (isset($this->modelData[$this->collection_key])
  19          && is_array($this->modelData[$this->collection_key])) {
  20        reset($this->modelData[$this->collection_key]);
  21      }
  22    }
  23  
  24    #[\ReturnTypeWillChange]
  25    public function current()
  26    {
  27      $this->coerceType($this->key());
  28      if (is_array($this->modelData[$this->collection_key])) {
  29        return current($this->modelData[$this->collection_key]);
  30      }
  31      return null;
  32    }
  33  
  34    #[\ReturnTypeWillChange]
  35    public function key()
  36    {
  37      if (isset($this->modelData[$this->collection_key])
  38          && is_array($this->modelData[$this->collection_key])) {
  39        return key($this->modelData[$this->collection_key]);
  40      }
  41      return null;
  42    }
  43  
  44    public function next(): void
  45    {
  46      next($this->modelData[$this->collection_key]);
  47    }
  48  
  49    public function valid(): bool
  50    {
  51      $key = $this->key();
  52      return $key !== null && $key !== false;
  53    }
  54  
  55    public function count(): int
  56    {
  57      if (!isset($this->modelData[$this->collection_key])) {
  58        return 0;
  59      }
  60      return count($this->modelData[$this->collection_key]);
  61    }
  62  
  63    public function offsetExists($offset): bool
  64    {
  65      if (!is_numeric($offset)) {
  66        return parent::offsetExists($offset);
  67      }
  68      return isset($this->modelData[$this->collection_key][$offset]);
  69    }
  70  
  71    #[\ReturnTypeWillChange]
  72    public function offsetGet($offset)
  73    {
  74      if (!is_numeric($offset)) {
  75        return parent::offsetGet($offset);
  76      }
  77      $this->coerceType($offset);
  78      return $this->modelData[$this->collection_key][$offset];
  79    }
  80  
  81    public function offsetSet($offset, $value): void
  82    {
  83      if (!is_numeric($offset)) {
  84        parent::offsetSet($offset, $value);
  85        return;
  86      }
  87      $this->modelData[$this->collection_key][$offset] = $value;
  88    }
  89  
  90    public function offsetUnset($offset): void
  91    {
  92      if (!is_numeric($offset)) {
  93          parent::offsetUnset($offset);
  94          return;
  95      }
  96      unset($this->modelData[$this->collection_key][$offset]);
  97    }
  98  
  99    private function coerceType($offset)
 100    {
 101      $typeKey = $this->keyType($this->collection_key);
 102      if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) {
 103        $type = $this->$typeKey;
 104        $this->modelData[$this->collection_key][$offset] =
 105            new $type($this->modelData[$this->collection_key][$offset]);
 106      }
 107    }
 108  }