1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2019 - present |
4
|
|
|
* updown - AbstractCollection.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 15/2/2019 |
8
|
|
|
* MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\UpDown\Abstracts; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractCollection |
15
|
|
|
* @package Biscolab\UpDown\Abstracts |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractCollection |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $items = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $index = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $children_class = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* AbstractCollection constructor. |
37
|
|
|
* |
38
|
|
|
* @param null|array $items |
39
|
|
|
*/ |
40
|
|
|
public function __construct(?array $items = []) |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
$this->setItems($items); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $items |
48
|
|
|
* |
49
|
|
|
* @return AbstractCollection |
50
|
|
|
*/ |
51
|
|
|
protected function setItems(?array $items = []) |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
if (is_array($items) && count($items)) { |
55
|
|
|
foreach ($items as $item) { |
56
|
|
|
$this->addItem($item); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $item |
65
|
|
|
* |
66
|
|
|
* @return AbstractCollection |
67
|
|
|
*/ |
68
|
|
|
public function addItem($item) |
69
|
|
|
{ |
70
|
|
|
|
71
|
|
|
$item = $this->parseItem($item); |
72
|
|
|
array_push($this->items, $item); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $item |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
protected function parseItem($item) |
83
|
|
|
{ |
84
|
|
|
|
85
|
|
|
if ($this->children_class && !$item instanceof $this->children_class) { |
86
|
|
|
$item = new $this->children_class($item); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $item; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function toJson(): string |
96
|
|
|
{ |
97
|
|
|
|
98
|
|
|
return json_encode($this->toArray()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function toArray(): array |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
return $this->items; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function __toString(): string |
114
|
|
|
{ |
115
|
|
|
|
116
|
|
|
return implode(',', $this->toArray()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return the current position of the index |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
|
|
public function position(): int |
125
|
|
|
{ |
126
|
|
|
|
127
|
|
|
return $this->index; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return the current object |
132
|
|
|
* |
133
|
|
|
* @return mixed|null |
134
|
|
|
*/ |
135
|
|
|
public function current() |
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
return $this->get($this->index); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $index |
143
|
|
|
* |
144
|
|
|
* @return mixed|null |
145
|
|
|
*/ |
146
|
|
|
public function get(int $index) |
147
|
|
|
{ |
148
|
|
|
|
149
|
|
|
return isset($this->items[$index]) ? $this->items[$index] : null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Move index to first position and return current element |
154
|
|
|
* |
155
|
|
|
* @return mixed|null |
156
|
|
|
*/ |
157
|
|
|
public function first() |
158
|
|
|
{ |
159
|
|
|
|
160
|
|
|
return $this->seek(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Move the index at the specified position |
165
|
|
|
* |
166
|
|
|
* @param int|null $index |
167
|
|
|
* |
168
|
|
|
* @return mixed|null |
169
|
|
|
*/ |
170
|
|
|
public function seek(?int $index = 0) |
171
|
|
|
{ |
172
|
|
|
|
173
|
|
|
$this->index = ($index < $this->count()) ? $index : $this->getLastIndex(); |
174
|
|
|
|
175
|
|
|
return $this->get(intval($this->index)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
public function count(): int |
182
|
|
|
{ |
183
|
|
|
|
184
|
|
|
return count($this->items); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
|
|
public function getLastIndex(): int |
191
|
|
|
{ |
192
|
|
|
|
193
|
|
|
$last_position = $this->count() - 1; |
194
|
|
|
|
195
|
|
|
return ($last_position) < 0 ? 0 : $last_position; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Move index at the end of collection and return current element |
200
|
|
|
* |
201
|
|
|
* @return mixed|null |
202
|
|
|
*/ |
203
|
|
|
public function last() |
204
|
|
|
{ |
205
|
|
|
|
206
|
|
|
return $this->seek($this->getLastIndex()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |