Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
#[AllowDynamicProperties]
13
class Google_Collection extends Google_Model implements Iterator, Countable
14
{
15
  protected $collection_key = 'items';
16
 
17
  public function rewind(): void
18
  {
19
    if (isset($this->modelData[$this->collection_key])
20
        && is_array($this->modelData[$this->collection_key])) {
21
      reset($this->modelData[$this->collection_key]);
22
    }
23
  }
24
 
25
  #[\ReturnTypeWillChange]
26
  public function current()
27
  {
28
    $this->coerceType($this->key());
29
    if (is_array($this->modelData[$this->collection_key])) {
30
      return current($this->modelData[$this->collection_key]);
31
    }
32
    return null;
33
  }
34
 
35
  #[\ReturnTypeWillChange]
36
  public function key()
37
  {
38
    if (isset($this->modelData[$this->collection_key])
39
        && is_array($this->modelData[$this->collection_key])) {
40
      return key($this->modelData[$this->collection_key]);
41
    }
42
    return null;
43
  }
44
 
45
  public function next(): void
46
  {
47
    next($this->modelData[$this->collection_key]);
48
  }
49
 
50
  public function valid(): bool
51
  {
52
    $key = $this->key();
53
    return $key !== null && $key !== false;
54
  }
55
 
56
  public function count(): int
57
  {
58
    if (!isset($this->modelData[$this->collection_key])) {
59
      return 0;
60
    }
61
    return count($this->modelData[$this->collection_key]);
62
  }
63
 
64
  public function offsetExists($offset): bool
65
  {
66
    if (!is_numeric($offset)) {
67
      return parent::offsetExists($offset);
68
    }
69
    return isset($this->modelData[$this->collection_key][$offset]);
70
  }
71
 
72
  #[\ReturnTypeWillChange]
73
  public function offsetGet($offset)
74
  {
75
    if (!is_numeric($offset)) {
76
      return parent::offsetGet($offset);
77
    }
78
    $this->coerceType($offset);
79
    return $this->modelData[$this->collection_key][$offset];
80
  }
81
 
82
  public function offsetSet($offset, $value): void
83
  {
84
    if (!is_numeric($offset)) {
85
      parent::offsetSet($offset, $value);
86
      return;
87
    }
88
    $this->modelData[$this->collection_key][$offset] = $value;
89
  }
90
 
91
  public function offsetUnset($offset): void
92
  {
93
    if (!is_numeric($offset)) {
94
        parent::offsetUnset($offset);
95
        return;
96
    }
97
    unset($this->modelData[$this->collection_key][$offset]);
98
  }
99
 
100
  private function coerceType($offset)
101
  {
102
    $typeKey = $this->keyType($this->collection_key);
103
    if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) {
104
      $type = $this->$typeKey;
105
      $this->modelData[$this->collection_key][$offset] =
106
          new $type($this->modelData[$this->collection_key][$offset]);
107
    }
108
  }
109
}