PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.2
WP-Stateless – Google Cloud Storage v3.2.2
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / src / Collection.php

Collection.php in WP-Stateless – Google Cloud Storage 3.2.2, at lib/Google/src/Collection.php

122 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Google;
4
5 /**
6 * Extension to the regular 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 Collection extends Model implements \Iterator, \Countable
11 {
12 protected $collection_key = 'items';
13
14 /** @return void */
15 #[\ReturnTypeWillChange]
16 public function rewind()
17 {
18 if (
19 isset($this->{$this->collection_key})
20 && is_array($this->{$this->collection_key})
21 ) {
22 reset($this->{$this->collection_key});
23 }
24 }
25
26 /** @return mixed */
27 #[\ReturnTypeWillChange]
28 public function current()
29 {
30 $this->coerceType($this->key());
31 if (is_array($this->{$this->collection_key})) {
32 return current($this->{$this->collection_key});
33 }
34 }
35
36 /** @return mixed */
37 #[\ReturnTypeWillChange]
38 public function key()
39 {
40 if (
41 isset($this->{$this->collection_key})
42 && is_array($this->{$this->collection_key})
43 ) {
44 return key($this->{$this->collection_key});
45 }
46 }
47
48 /** @return mixed */
49 #[\ReturnTypeWillChange]
50 public function next()
51 {
52 return next($this->{$this->collection_key});
53 }
54
55 /** @return bool */
56 #[\ReturnTypeWillChange]
57 public function valid()
58 {
59 $key = $this->key();
60 return $key !== null && $key !== false;
61 }
62
63 /** @return int */
64 #[\ReturnTypeWillChange]
65 public function count()
66 {
67 if (!isset($this->{$this->collection_key})) {
68 return 0;
69 }
70 return count($this->{$this->collection_key});
71 }
72
73 /** @return bool */
74 #[\ReturnTypeWillChange]
75 public function offsetExists($offset)
76 {
77 if (!is_numeric($offset)) {
78 return parent::offsetExists($offset);
79 }
80 return isset($this->{$this->collection_key}[$offset]);
81 }
82
83 /** @return mixed */
84 public function offsetGet($offset)
85 {
86 if (!is_numeric($offset)) {
87 return parent::offsetGet($offset);
88 }
89 $this->coerceType($offset);
90 return $this->{$this->collection_key}[$offset];
91 }
92
93 /** @return void */
94 #[\ReturnTypeWillChange]
95 public function offsetSet($offset, $value)
96 {
97 if (!is_numeric($offset)) {
98 parent::offsetSet($offset, $value);
99 }
100 $this->{$this->collection_key}[$offset] = $value;
101 }
102
103 /** @return void */
104 #[\ReturnTypeWillChange]
105 public function offsetUnset($offset)
106 {
107 if (!is_numeric($offset)) {
108 parent::offsetUnset($offset);
109 }
110 unset($this->{$this->collection_key}[$offset]);
111 }
112
113 private function coerceType($offset)
114 {
115 $keyType = $this->keyType($this->collection_key);
116 if ($keyType && !is_object($this->{$this->collection_key}[$offset])) {
117 $this->{$this->collection_key}[$offset] =
118 new $keyType($this->{$this->collection_key}[$offset]);
119 }
120 }
121 }
122