Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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()
  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    public function current()
  25    {
  26      $this->coerceType($this->key());
  27      if (is_array($this->modelData[$this->collection_key])) {
  28        return current($this->modelData[$this->collection_key]);
  29      }
  30    }
  31  
  32    public function key()
  33    {
  34      if (isset($this->modelData[$this->collection_key])
  35          && is_array($this->modelData[$this->collection_key])) {
  36        return key($this->modelData[$this->collection_key]);
  37      }
  38    }
  39  
  40    public function next()
  41    {
  42      return next($this->modelData[$this->collection_key]);
  43    }
  44  
  45    public function valid()
  46    {
  47      $key = $this->key();
  48      return $key !== null && $key !== false;
  49    }
  50  
  51    public function count()
  52    {
  53      if (!isset($this->modelData[$this->collection_key])) {
  54        return 0;
  55      }
  56      return count($this->modelData[$this->collection_key]);
  57    }
  58  
  59    public function offsetExists($offset)
  60    {
  61      if (!is_numeric($offset)) {
  62        return parent::offsetExists($offset);
  63      }
  64      return isset($this->modelData[$this->collection_key][$offset]);
  65    }
  66  
  67    public function offsetGet($offset)
  68    {
  69      if (!is_numeric($offset)) {
  70        return parent::offsetGet($offset);
  71      }
  72      $this->coerceType($offset);
  73      return $this->modelData[$this->collection_key][$offset];
  74    }
  75  
  76    public function offsetSet($offset, $value)
  77    {
  78      if (!is_numeric($offset)) {
  79        return parent::offsetSet($offset, $value);
  80      }
  81      $this->modelData[$this->collection_key][$offset] = $value;
  82    }
  83  
  84    public function offsetUnset($offset)
  85    {
  86      if (!is_numeric($offset)) {
  87        return parent::offsetUnset($offset);
  88      }
  89      unset($this->modelData[$this->collection_key][$offset]);
  90    }
  91  
  92    private function coerceType($offset)
  93    {
  94      $typeKey = $this->keyType($this->collection_key);
  95      if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) {
  96        $type = $this->$typeKey;
  97        $this->modelData[$this->collection_key][$offset] =
  98            new $type($this->modelData[$this->collection_key][$offset]);
  99      }
 100    }
 101  }