Passed
Branch master (41ef34)
by Roberto
04:08
created

AbstractCollection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 25
dl 0
loc 165
c 0
b 0
f 0
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A toJson() 0 3 1
A toArray() 0 3 1
A position() 0 3 1
A seek() 0 5 2
A first() 0 3 1
A current() 0 3 1
A count() 0 3 1
A getLastIndex() 0 5 2
A get() 0 3 2
A __toString() 0 3 1
A parseItem() 0 3 1
A __construct() 0 3 1
A setItems() 0 9 4
A last() 0 3 1
A addItem() 0 6 1
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 {
18
19
	/**
20
	 * @var array
21
	 */
22
	protected $items = [];
23
24
	/**
25
	 * @var int
26
	 */
27
	protected $index = 0;
28
29
	/**
30
	 * AbstractCollection constructor.
31
	 *
32
	 * @param null|array $items
33
	 */
34
	public function __construct(?array $items = []) {
35
36
		$this->setItems($items);
37
	}
38
39
	/**
40
	 * @param array $items
41
	 *
42
	 * @return AbstractCollection
43
	 */
44
	protected function setItems(?array $items = []) {
45
46
		if (is_array($items) && count($items)) {
47
			foreach ($items as $item) {
48
				$this->addItem($item);
49
			}
50
		}
51
52
		return $this;
53
	}
54
55
	/**
56
	 * @param $item
57
	 *
58
	 * @return AbstractCollection
59
	 */
60
	public function addItem($item) {
61
62
		$item = $this->parseItem($item);
63
		array_push($this->items, $item);
64
65
		return $this;
66
	}
67
68
	/**
69
	 * @param $item
70
	 *
71
	 * @return mixed
72
	 */
73
	protected function parseItem($item) {
74
75
		return $item;
76
	}
77
78
	/**
79
	 * @return array
80
	 */
81
	public function toArray(): array {
82
83
		return $this->items;
84
	}
85
86
	/**
87
	 * @param $index
88
	 *
89
	 * @return mixed|null
90
	 */
91
	public function get(int $index) {
92
93
		return isset($this->items[$index]) ? $this->items[$index] : null;
94
	}
95
96
	/**
97
	 * @return int
98
	 */
99
	public function count(): int {
100
101
		return count($this->items);
102
	}
103
104
	/**
105
	 * @return string
106
	 */
107
	public function toJson(): string {
108
109
		return json_encode($this->toArray());
110
	}
111
112
	/**
113
	 * @return string
114
	 */
115
	public function __toString(): string {
116
117
		return implode(',', $this->toArray());
118
	}
119
120
	/**
121
	 * @return int
122
	 */
123
	public function getLastIndex(): int {
124
125
		$last_position = $this->count() - 1;
126
127
		return ($last_position) < 0 ? 0 : $last_position;
128
	}
129
130
	/**
131
	 * Move the index at the specified position
132
	 *
133
	 * @param int|null $index
134
	 *
135
	 * @return mixed|null
136
	 */
137
	public function seek(?int $index = 0) {
138
139
		$this->index = ($index < $this->count()) ? $index : $this->getLastIndex();
140
141
		return $this->get($this->index);
0 ignored issues
show
Bug introduced by
It seems like $this->index can also be of type null; however, parameter $index of Biscolab\GoogleMaps\Abst...stractCollection::get() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
		return $this->get(/** @scrutinizer ignore-type */ $this->index);
Loading history...
142
	}
143
144
	/**
145
	 * Return the current position of the index
146
	 *
147
	 * @return int
148
	 */
149
	public function position(): int {
150
151
		return $this->index;
152
	}
153
154
	/**
155
	 * Return the current object
156
	 *
157
	 * @return mixed|null
158
	 */
159
	public function current() {
160
161
		return $this->get($this->index);
162
	}
163
164
	/**
165
	 * Move index to first position and return current element
166
	 *
167
	 * @return mixed|null
168
	 */
169
	public function first() {
170
171
		return $this->seek();
172
	}
173
174
	/**
175
	 * Move index at the end of collection and return current element
176
	 *
177
	 * @return mixed|null
178
	 */
179
	public function last() {
180
181
		return $this->seek($this->getLastIndex());
182
	}
183
184
}