|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
|
5
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
|
6
|
|
|
* |
|
7
|
|
|
* Licensed under The MIT License |
|
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
10
|
|
|
* |
|
11
|
|
|
* @link https://github.com/knut7/framework/ for the canonical source repository |
|
12
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
|
13
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
|
14
|
|
|
* @author Marcio Zebedeu - [email protected] |
|
15
|
|
|
* @version 1.0.2 |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Ballybran\Core\Collections\Collection; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
use Ballybran\Core\Variables\Variable; |
|
22
|
|
|
use Closure; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class IteratorCollection |
|
26
|
|
|
* @package Ballybran\Core\Collections\Collection |
|
27
|
|
|
*/ |
|
28
|
|
|
class IteratorCollection extends Variable implements \ArrayAccess |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $elements; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
|
|
39
|
|
|
//put your code here |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* IteratorCollection constructor. |
|
43
|
|
|
* @param array $elements |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(array $elements = array()) |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
parent::__construct($elements); |
|
49
|
|
|
|
|
50
|
|
|
$this->elements = $elements; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function setElementsFromTrustedSource(array $elements) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->elements = $elements; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function toArray(): array |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->elements; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return \ArrayObject |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getIterator() |
|
71
|
|
|
{ |
|
72
|
|
|
|
|
73
|
|
|
return new \ArrayObject($this->elements); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function count() |
|
77
|
|
|
{ |
|
78
|
|
|
return count($this->elements); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function current() |
|
82
|
|
|
{ |
|
83
|
|
|
return current($this->elements); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function contains($element) |
|
87
|
|
|
{ |
|
88
|
|
|
return in_array($element, $this->elements, true); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
|
|
public function next() |
|
95
|
|
|
{ |
|
96
|
|
|
return next($this->elements); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function last() |
|
100
|
|
|
{ |
|
101
|
|
|
return end($this->elements); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function first() |
|
105
|
|
|
{ |
|
106
|
|
|
return reset($this->elements); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function key() |
|
110
|
|
|
{ |
|
111
|
|
|
return key($this->elements); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function valid() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->offsetExists($this->elements); |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function offsetGet($offset) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->get($offset); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function offsetSet($offset, $value) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!isset($offset)) { |
|
128
|
|
|
$this->set($offset, $value); |
|
129
|
|
|
} |
|
130
|
|
|
$this->set($offset, $value); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function offsetUnset($offset) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->remove($offset); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function containsKey($key) |
|
139
|
|
|
{ |
|
140
|
|
|
return isset($this->elements[$key]) || array_key_exists($key, $this->elements); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function offsetExists($offset) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->containsKey($offset); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function remove($key) |
|
149
|
|
|
{ |
|
150
|
|
|
if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) { |
|
151
|
|
|
return false; |
|
152
|
|
|
} else { |
|
153
|
|
|
$removed = $this->elements[$key]; |
|
154
|
|
|
unset($this->elements[$key]); |
|
155
|
|
|
|
|
156
|
|
|
return $removed; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function removeEleme($element) |
|
161
|
|
|
{ |
|
162
|
|
|
$key = array_search($element, $this->elements, true); |
|
163
|
|
|
if (false === $key) { |
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
unset($this->elements[$key]); |
|
167
|
|
|
return true; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function add($value) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->elements[] = $value; |
|
173
|
|
|
|
|
174
|
|
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function set($key, $value) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->elements[$key] = $value; |
|
180
|
|
|
|
|
181
|
|
|
return true; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function ksort() |
|
185
|
|
|
{ |
|
186
|
|
|
|
|
187
|
|
|
return ksort($this->elements); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function natSort() |
|
191
|
|
|
{ |
|
192
|
|
|
return natsort($this->elements); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function natcasesort() |
|
196
|
|
|
{ |
|
197
|
|
|
return natcasesort($this->elements); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function exists(Closure $p) |
|
201
|
|
|
{ |
|
202
|
|
|
foreach ($this->elements as $key => $element) { |
|
203
|
|
|
if ($p($key, $element)) { |
|
204
|
|
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return int|string |
|
212
|
|
|
* return the position of the element |
|
213
|
|
|
*/ |
|
214
|
|
|
public function indexOf($element) |
|
215
|
|
|
{ |
|
216
|
|
|
return array_search($element, $this->elements); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function isEmpty() |
|
220
|
|
|
{ |
|
221
|
|
|
return empty($this->elements); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function getValues() |
|
225
|
|
|
{ |
|
226
|
|
|
|
|
227
|
|
|
return array_values($this->elements); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function getKey() |
|
231
|
|
|
{ |
|
232
|
|
|
return array_keys($this->elements); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function get($key) |
|
236
|
|
|
{ |
|
237
|
|
|
return isset($this->elements[$key]) ? $this->elements[$key] : null; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
public function slice($start, $end) |
|
242
|
|
|
{ |
|
243
|
|
|
if ($start < 0 || !is_int($start)) { |
|
244
|
|
|
throw new \InvalidArgumentException("Start must be a no-negative integer"); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
if ($end < 0 || !is_int($end)) { |
|
248
|
|
|
throw new \InvalidArgumentException("End must be a positive integer"); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
if ($start > $end) { |
|
252
|
|
|
throw new \InvalidArgumentException("End must be geater than start"); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
if ($end > $this->count() + 1) { |
|
256
|
|
|
throw new \InvalidArgumentException("End must be less than the count of the items in the Collection"); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$length = $end - $start + 1; |
|
260
|
|
|
|
|
261
|
|
|
$subsetItems = array_slice($this->elements, $start, $length); |
|
262
|
|
|
|
|
263
|
|
|
if(null === $subsetItems ){ |
|
264
|
|
|
return null; |
|
265
|
|
|
} |
|
266
|
|
|
return $this->setElementsFromTrustedSource($subsetItems); |
|
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function reverse() |
|
271
|
|
|
{ |
|
272
|
|
|
$item = array_reverse($this->elements); |
|
273
|
|
|
if(null === $item ){ |
|
274
|
|
|
return null; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
return $this->setElementsFromTrustedSource($item); |
|
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
public function find($value) |
|
282
|
|
|
{ |
|
283
|
|
|
$this->get($value); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
} |
|
288
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.