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 / Google / Collection.php

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

95 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once $GLOBALS['iwp_mmb_plugin_dir']."/lib/Google/Model.php";
4
5 /**
6 * Extension to the regular IWP_google_Model that automatically
7 * exposes the items array for iteration, so you can just
8 * iterate over the object rather than a reference inside.
9 */
10 class IWP_google_Collection extends IWP_google_Model implements Iterator, Countable
11 {
12 protected $collection_key = 'items';
13 #[\ReturnTypeWillChange]
14 public function rewind()
15 {
16 if (is_array($this->modelData[$this->collection_key])) {
17 reset($this->modelData[$this->collection_key]);
18 }
19 }
20 #[\ReturnTypeWillChange]
21 public function current()
22 {
23 $this->coerceType($this->key());
24 if (is_array($this->modelData[$this->collection_key])) {
25 return current($this->modelData[$this->collection_key]);
26 }
27 }
28 #[\ReturnTypeWillChange]
29 public function key()
30 {
31 if (is_array($this->modelData[$this->collection_key])) {
32 return key($this->modelData[$this->collection_key]);
33 }
34 }
35 #[\ReturnTypeWillChange]
36 public function next()
37 {
38 return next($this->modelData[$this->collection_key]);
39 }
40 #[\ReturnTypeWillChange]
41 public function valid()
42 {
43 $key = $this->key();
44 return $key !== null && $key !== false;
45 }
46 #[\ReturnTypeWillChange]
47 public function count()
48 {
49 return count($this->modelData[$this->collection_key]);
50 }
51
52 public function offsetExists ($offset)
53 {
54 if (!is_numeric($offset)) {
55 return parent::offsetExists($offset);
56 }
57 return isset($this->modelData[$this->collection_key][$offset]);
58 }
59
60 public function offsetGet($offset)
61 {
62 if (!is_numeric($offset)) {
63 return parent::offsetGet($offset);
64 }
65 $this->coerceType($offset);
66 return $this->modelData[$this->collection_key][$offset];
67 }
68
69 public function offsetSet($offset, $value)
70 {
71 if (!is_numeric($offset)) {
72 return parent::offsetSet($offset, $value);
73 }
74 $this->modelData[$this->collection_key][$offset] = $value;
75 }
76
77 public function offsetUnset($offset)
78 {
79 if (!is_numeric($offset)) {
80 return parent::offsetUnset($offset);
81 }
82 unset($this->modelData[$this->collection_key][$offset]);
83 }
84
85 private function coerceType($offset)
86 {
87 $typeKey = $this->keyType($this->collection_key);
88 if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) {
89 $type = $this->$typeKey;
90 $this->modelData[$this->collection_key][$offset] =
91 new $type($this->modelData[$this->collection_key][$offset]);
92 }
93 }
94 }
95