biscolab /
google-maps-php-sdk
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Copyright (c) 2018 - present |
||
| 4 | * Google Maps PHP - AbstractCollection.php |
||
| 5 | * author: Roberto Belotti - [email protected] |
||
| 6 | * web : robertobelotti.com, github.com/biscolab |
||
| 7 | * Initial version created on: 5/9/2018 |
||
| 8 | * MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Biscolab\GoogleMaps\Abstracts; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class AbstractCollection |
||
| 15 | * @package Biscolab\GoogleMaps\Abstracts |
||
| 16 | */ |
||
| 17 | abstract class AbstractCollection implements \Iterator, \Countable |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $items = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected $index = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * AbstractCollection constructor. |
||
| 32 | * |
||
| 33 | * @param null|array $items |
||
| 34 | */ |
||
| 35 | 8 | public function __construct(?array $items = []) |
|
| 36 | { |
||
| 37 | |||
| 38 | 8 | $this->setItems($items); |
|
| 39 | 8 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param array $items |
||
| 43 | * |
||
| 44 | * @return AbstractCollection |
||
| 45 | */ |
||
| 46 | 8 | protected function setItems(?array $items = []) |
|
| 47 | { |
||
| 48 | |||
| 49 | 8 | if (is_array($items) && count($items)) { |
|
| 50 | 6 | foreach ($items as $item) { |
|
| 51 | 6 | $this->addItem($item); |
|
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | 8 | return $this; |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param $item |
||
| 60 | * |
||
| 61 | * @return AbstractCollection |
||
| 62 | */ |
||
| 63 | 8 | public function addItem($item) |
|
| 64 | { |
||
| 65 | |||
| 66 | 8 | $item = $this->parseItem($item); |
|
| 67 | 8 | array_push($this->items, $item); |
|
| 68 | |||
| 69 | 8 | return $this; |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param $item |
||
| 74 | * |
||
| 75 | * @return mixed |
||
| 76 | */ |
||
| 77 | protected function parseItem($item) |
||
| 78 | { |
||
| 79 | |||
| 80 | return $item; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function toJson(): string |
||
| 87 | { |
||
| 88 | |||
| 89 | return json_encode($this->toArray()); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | 3 | public function toArray(): array |
|
| 96 | { |
||
| 97 | |||
| 98 | 3 | return $this->items; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function __toString(): string |
||
| 105 | { |
||
| 106 | |||
| 107 | return implode(',', $this->toArray()); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Return the current position of the index |
||
| 112 | * |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | public function position(): int |
||
| 116 | { |
||
| 117 | |||
| 118 | return $this->index; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Move index to first position and return current element |
||
| 123 | * |
||
| 124 | * @return mixed|null |
||
| 125 | */ |
||
| 126 | 6 | public function first() |
|
| 127 | { |
||
| 128 | |||
| 129 | 6 | return $this->get(0); |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $index |
||
| 134 | * |
||
| 135 | * @return mixed|null |
||
| 136 | */ |
||
| 137 | 7 | public function get(int $index) |
|
| 138 | { |
||
| 139 | |||
| 140 | 7 | return isset($this->items[$index]) ? $this->items[$index] : null; |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Move the index at the specified position |
||
| 145 | * |
||
| 146 | * @param int|null $index |
||
| 147 | * |
||
| 148 | * @return mixed|null |
||
| 149 | */ |
||
| 150 | 1 | public function seek(?int $index = 0) |
|
| 151 | { |
||
| 152 | |||
| 153 | 1 | $this->index = ($index < $this->count()) ? $index : $this->getLastIndex(); |
|
| 154 | |||
| 155 | 1 | return $this->get(intval($this->index)); |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return int |
||
| 160 | */ |
||
| 161 | 7 | public function count(): int |
|
| 162 | { |
||
| 163 | |||
| 164 | 7 | return count($this->items); |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | 1 | public function getLastIndex(): int |
|
| 171 | { |
||
| 172 | |||
| 173 | 1 | $last_position = $this->count() - 1; |
|
| 174 | |||
| 175 | 1 | return ($last_position) < 0 ? 0 : $last_position; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Move index at the end of collection and return current element |
||
| 180 | * |
||
| 181 | * @return mixed|null |
||
| 182 | */ |
||
| 183 | 1 | public function last() |
|
| 184 | { |
||
| 185 | |||
| 186 | 1 | return $this->get($this->getLastIndex()); |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * |
||
| 191 | * @return mixed|null |
||
| 192 | */ |
||
| 193 | 1 | public function current() |
|
| 194 | { |
||
| 195 | |||
| 196 | 1 | return $this->get($this->index); |
|
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Move index to next position and return current element |
||
| 201 | * |
||
| 202 | * @return mixed|null |
||
| 203 | */ |
||
| 204 | public function next() |
||
| 205 | { |
||
| 206 | |||
| 207 | ++$this->index; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Return current key/index |
||
| 212 | * |
||
| 213 | * @return mixed|null |
||
| 214 | */ |
||
| 215 | public function key() |
||
| 216 | { |
||
| 217 | |||
| 218 | return $this->index; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Return current key/index |
||
| 223 | * |
||
| 224 | * @return mixed|null |
||
| 225 | */ |
||
| 226 | public function valid() |
||
| 227 | { |
||
| 228 | |||
| 229 | return !empty($this->current()); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Move index to first position and return current element |
||
| 234 | * |
||
| 235 | * @return mixed|null |
||
| 236 | */ |
||
| 237 | public function rewind() |
||
| 238 | { |
||
| 239 | |||
| 240 | return $this->index = 0; |
||
|
0 ignored issues
–
show
|
|||
| 241 | } |
||
| 242 | |||
| 243 | } |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: