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 | #[\ReturnTypeWillChange]
|
---|
85 | public function offsetGet($offset)
|
---|
86 | {
|
---|
87 | if (!is_numeric($offset)) {
|
---|
88 | return parent::offsetGet($offset);
|
---|
89 | }
|
---|
90 | $this->coerceType($offset);
|
---|
91 | return $this->{$this->collection_key}[$offset];
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** @return void */
|
---|
95 | #[\ReturnTypeWillChange]
|
---|
96 | public function offsetSet($offset, $value)
|
---|
97 | {
|
---|
98 | if (!is_numeric($offset)) {
|
---|
99 | parent::offsetSet($offset, $value);
|
---|
100 | }
|
---|
101 | $this->{$this->collection_key}[$offset] = $value;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** @return void */
|
---|
105 | #[\ReturnTypeWillChange]
|
---|
106 | public function offsetUnset($offset)
|
---|
107 | {
|
---|
108 | if (!is_numeric($offset)) {
|
---|
109 | parent::offsetUnset($offset);
|
---|
110 | }
|
---|
111 | unset($this->{$this->collection_key}[$offset]);
|
---|
112 | }
|
---|
113 |
|
---|
114 | private function coerceType($offset)
|
---|
115 | {
|
---|
116 | $keyType = $this->keyType($this->collection_key);
|
---|
117 | if ($keyType && !is_object($this->{$this->collection_key}[$offset])) {
|
---|
118 | $this->{$this->collection_key}[$offset] =
|
---|
119 | new $keyType($this->{$this->collection_key}[$offset]);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|