-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathIterator.php
259 lines (224 loc) · 5.46 KB
/
Iterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* @package Grav.Common
*
* @copyright Copyright (C) 2014 - 2016 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Common;
use RocketTheme\Toolbox\ArrayTraits\ArrayAccessWithGetters;
use RocketTheme\Toolbox\ArrayTraits\Iterator as ArrayIterator;
use RocketTheme\Toolbox\ArrayTraits\Constructor;
use RocketTheme\Toolbox\ArrayTraits\Countable;
use RocketTheme\Toolbox\ArrayTraits\Export;
use RocketTheme\Toolbox\ArrayTraits\Serializable;
class Iterator implements \ArrayAccess, \Iterator, \Countable, \Serializable
{
use Constructor, ArrayAccessWithGetters, ArrayIterator, Countable, Serializable, Export;
/**
* @var array
*/
protected $items = [];
/**
* Convert function calls for the existing keys into their values.
*
* @param string $key
* @param mixed $args
*
* @return mixed
*/
public function __call($key, $args)
{
return (isset($this->items[$key])) ? $this->items[$key] : null;
}
/**
* Clone the iterator.
*/
public function __clone()
{
foreach ($this as $key => $value) {
if (is_object($value)) {
$this->$key = clone $this->$key;
}
}
}
/**
* Convents iterator to a comma separated list.
*
* @return string
*/
public function __toString()
{
return implode(',', $this->items);
}
/**
* Remove item from the list.
*
* @param $key
*/
public function remove($key)
{
$this->offsetUnset($key);
}
/**
* Return previous item.
*
* @return mixed
*/
public function prev()
{
return prev($this->items);
}
/**
* Return nth item.
*
* @param int $key
*
* @return mixed|bool
*/
public function nth($key)
{
$items = array_keys($this->items);
return (isset($items[$key])) ? $this->offsetGet($items[$key]) : false;
}
/**
* Get the first item
*
* @return mixed
*/
public function first()
{
$items = array_keys($this->items);
return $this->offsetGet(array_shift($items));
}
/**
* Get the last item
*
* @return mixed
*/
public function last()
{
$items = array_keys($this->items);
return $this->offsetGet(array_pop($items));
}
/**
* Reverse the Iterator
*
* @return $this
*/
public function reverse()
{
$this->items = array_reverse($this->items);
return $this;
}
/**
* @param mixed $needle Searched value.
*
* @return string|bool Key if found, otherwise false.
*/
public function indexOf($needle)
{
foreach (array_values($this->items) as $key => $value) {
if ($value === $needle) {
return $key;
}
}
return false;
}
/**
* Shuffle items.
*
* @return $this
*/
public function shuffle()
{
$keys = array_keys($this->items);
shuffle($keys);
$new = [];
foreach ($keys as $key) {
$new[$key] = $this->items[$key];
}
$this->items = $new;
return $this;
}
/**
* Slice the list.
*
* @param int $offset
* @param int $length
*
* @return $this
*/
public function slice($offset, $length = null)
{
$this->items = array_slice($this->items, $offset, $length);
return $this;
}
/**
* Pick one or more random entries.
*
* @param int $num Specifies how many entries should be picked.
*
* @return $this
*/
public function random($num = 1)
{
$this->items = array_intersect_key($this->items, array_flip((array)array_rand($this->items, $num)));
return $this;
}
/**
* Append new elements to the list.
*
* @param array|Iterator $items Items to be appended. Existing keys will be overridden with the new values.
*
* @return $this
*/
public function append($items)
{
if ($items instanceof static) {
$items = $items->toArray();
}
$this->items = array_merge($this->items, (array)$items);
return $this;
}
/**
* Filter elements from the list
*
* @param callable|null $callback A function the receives ($value, $key) and must return a boolean to indicate
* filter status
*
* @return $this
*/
public function filter(callable $callback = null)
{
foreach ($this->items as $key => $value) {
if (
($callback && !call_user_func($callback, $value, $key)) ||
(!$callback && !(bool)$value)
) {
unset($this->items[$key]);
}
}
return $this;
}
/**
* Sorts elements from the list and returns a copy of the list in the proper order
*
* @param callable|null $callback
*
* @param bool $desc
*
* @return $this|array
* @internal param bool $asc
*
*/
public function sort(callable $callback = null, $desc = false)
{
if (!$callback || !is_callable($callback)) {
return $this;
}
$items = $this->items;
uasort($items, $callback);
return !$desc ? $items : array_reverse($items, true);
}
}