AccessToken
2 years ago
AuthHandler
2 years ago
Http
2 years ago
Service
2 years ago
Task
2 years ago
Utils
2 years ago
Client.php
2 years ago
Collection.php
2 years ago
Exception.php
2 years ago
Model.php
2 years ago
Service.php
2 years ago
aliases.php
2 years ago
Collection.php
122 lines
| 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 |