PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / lib / Google2 / Collection.php

Collection.php in InfiniteWP Client trunk, at lib/Google2/Collection.php

102 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 #[\ReturnTypeWillChange]
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 #[\ReturnTypeWillChange]
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 #[\ReturnTypeWillChange]
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 #[\ReturnTypeWillChange]
40 public function next()
41 {
42 return next($this->modelData[$this->collection_key]);
43 }
44 #[\ReturnTypeWillChange]
45 public function valid()
46 {
47 $key = $this->key();
48 return $key !== null && $key !== false;
49 }
50 #[\ReturnTypeWillChange]
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 #[\ReturnTypeWillChange]
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 #[\ReturnTypeWillChange]
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 #[\ReturnTypeWillChange]
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 }
102