1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace YaLinqo; |
4
|
|
|
|
5
|
|
|
use YaLinqo; |
6
|
|
|
|
7
|
|
|
trait EnumerablePagination |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Returns the value at a specified key in a sequence. |
11
|
|
|
* <p><b>Syntax</b>: elementAt (key) |
12
|
|
|
* <p>Returns the value at a specified key in a sequence. |
13
|
|
|
* <p>If the type of source iterator implements {@link ArrayAccess}, that implementation is used to obtain the value at the specified key. Otherwise, this method obtains the specified value. |
14
|
|
|
* <p>This method throws an exception if key is not found. To instead return a default value when the specified key is not found, use the {@link elementAtOrDefault} method. |
15
|
|
|
* @param mixed $key The key of the value to retrieve. |
16
|
|
|
* @throws \UnexpectedValueException If sequence does not contain value with specified key. |
17
|
|
|
* @return mixed The value at the key in the source sequence. |
18
|
|
|
* @package YaLinqo\Pagination |
19
|
|
|
*/ |
20
|
|
|
public function elementAt ($key) |
21
|
|
|
{ |
22
|
|
|
/** @var $it \Iterator|\ArrayAccess */ |
23
|
|
|
$it = $this->getIterator(); |
24
|
|
|
|
25
|
|
|
if ($it instanceof \ArrayAccess) { |
26
|
|
|
if (!$it->offsetExists($key)) |
27
|
|
|
throw new \UnexpectedValueException(Errors::NO_KEY); |
28
|
|
|
return $it->offsetGet($key); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
foreach ($it as $k => $v) { |
32
|
|
|
if ($k === $key) |
33
|
|
|
return $v; |
34
|
|
|
} |
35
|
|
|
throw new \UnexpectedValueException(Errors::NO_KEY); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the value at a specified key in a sequence or a default value if the key is not found. |
40
|
|
|
* <p><b>Syntax</b>: elementAtOrDefault (key [, default]) |
41
|
|
|
* <p>If the type of source iterator implements {@link ArrayAccess}, that implementation is used to obtain the value at the specified key. Otherwise, this method obtains the specified value. |
42
|
|
|
* @param mixed $key The key of the value to retrieve. |
43
|
|
|
* @param mixed $default Value to return if sequence does not contain value with specified key. Default: null. |
44
|
|
|
* @return mixed default value if the key is not found in the source sequence; otherwise, the value at the specified key in the source sequence. |
45
|
|
|
* @package YaLinqo\Pagination |
46
|
|
|
*/ |
47
|
|
|
public function elementAtOrDefault ($key, $default = null) |
48
|
|
|
{ |
49
|
|
|
/** @var $it \Iterator|\ArrayAccess */ |
50
|
|
|
$it = $this->getIterator(); |
51
|
|
|
|
52
|
|
|
if ($it instanceof \ArrayAccess) |
53
|
|
|
return $it->offsetExists($key) ? $it->offsetGet($key) : $default; |
54
|
|
|
|
55
|
|
|
foreach ($it as $k => $v) { |
56
|
|
|
if ($k === $key) |
57
|
|
|
return $v; |
58
|
|
|
} |
59
|
|
|
return $default; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the first element of a sequence. |
64
|
|
|
* <p><b>Syntax</b>: first () |
65
|
|
|
* <p>Returns the first element of a sequence. |
66
|
|
|
* <p>The first method throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the {@link firstOrDefault} method. |
67
|
|
|
* <p><b>Syntax</b>: first (predicate {(v, k) ==> result}) |
68
|
|
|
* <p>Returns the first element in a sequence that satisfies a specified condition. |
69
|
|
|
* <p>The first method throws an exception if no matching element is found in source. To instead return a default value when no matching element is found, use the {@link firstOrDefault} method. |
70
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
71
|
|
|
* @throws \UnexpectedValueException If source contains no matching elements. |
72
|
|
|
* @return mixed If predicate is null: the first element in the specified sequence. If predicate is not null: The first element in the sequence that passes the test in the specified predicate function. |
73
|
|
|
* @package YaLinqo\Pagination |
74
|
|
|
*/ |
75
|
|
|
public function first ($predicate = null) |
76
|
|
|
{ |
77
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
78
|
|
|
|
79
|
|
|
foreach ($this as $k => $v) { |
80
|
|
|
if ($predicate($v, $k)) |
81
|
|
|
return $v; |
82
|
|
|
} |
83
|
|
|
throw new \UnexpectedValueException(Errors::NO_MATCHES); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the first element of a sequence, or a default value if the sequence contains no elements. |
88
|
|
|
* <p><b>Syntax</b>: firstOrDefault ([default]) |
89
|
|
|
* <p>Returns the first element of a sequence, or a default value if the sequence contains no elements. |
90
|
|
|
* <p><b>Syntax</b>: firstOrDefault ([default [, predicate {(v, k) ==> result}]]) |
91
|
|
|
* <p>Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. |
92
|
|
|
* <p>If obtaining the default value is a costly operation, use {@link firstOrFallback} method to avoid overhead. |
93
|
|
|
* @param mixed $default A default value. |
94
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
95
|
|
|
* @return mixed If predicate is null: default value if source is empty; otherwise, the first element in source. If predicate is not null: default value if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate. |
96
|
|
|
* @package YaLinqo\Pagination |
97
|
|
|
*/ |
98
|
|
|
public function firstOrDefault ($default = null, $predicate = null) |
99
|
|
|
{ |
100
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
101
|
|
|
|
102
|
|
|
foreach ($this as $k => $v) { |
103
|
|
|
if ($predicate($v, $k)) |
104
|
|
|
return $v; |
105
|
|
|
} |
106
|
|
|
return $default; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns the first element of a sequence, or the result of calling a fallback function if the sequence contains no elements. |
111
|
|
|
* <p><b>Syntax</b>: firstOrFallback ([fallback {() ==> value}]) |
112
|
|
|
* <p>Returns the first element of a sequence, or the result of calling a fallback function if the sequence contains no elements. |
113
|
|
|
* <p><b>Syntax</b>: firstOrFallback ([fallback {() ==> value} [, predicate {(v, k) ==> result}]]) |
114
|
|
|
* <p>Returns the first element of the sequence that satisfies a condition or the result of calling a fallback function if no such element is found. |
115
|
|
|
* <p>The fallback function is not executed if a matching element is found. Use the firstOrFallback method if obtaining the default value is a costly operation to avoid overhead. Otherwise, use {@link firstOrDefault}. |
116
|
|
|
* @param callable $fallback {() ==> value} A fallback function to return the default element. |
117
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
118
|
|
|
* @return mixed If predicate is null: the result of calling a fallback function if source is empty; otherwise, the first element in source. If predicate is not null: the result of calling a fallback function if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate. |
119
|
|
|
* @package YaLinqo\Pagination |
120
|
|
|
*/ |
121
|
|
|
public function firstOrFallback ($fallback, $predicate = null) |
122
|
|
|
{ |
123
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
124
|
|
|
|
125
|
|
|
foreach ($this as $k => $v) { |
126
|
|
|
if ($predicate($v, $k)) |
127
|
|
|
return $v; |
128
|
|
|
} |
129
|
|
|
return $fallback(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the last element of a sequence. |
134
|
|
|
* <p><b>Syntax</b>: last () |
135
|
|
|
* <p>Returns the last element of a sequence. |
136
|
|
|
* <p>The last method throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the {@link lastOrDefault} method. |
137
|
|
|
* <p><b>Syntax</b>: last (predicate {(v, k) ==> result}) |
138
|
|
|
* <p>Returns the last element in a sequence that satisfies a specified condition. |
139
|
|
|
* <p>The last method throws an exception if no matching element is found in source. To instead return a default value when no matching element is found, use the {@link lastOrDefault} method. |
140
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
141
|
|
|
* @throws \UnexpectedValueException If source contains no matching elements. |
142
|
|
|
* @return mixed If predicate is null: the last element in the specified sequence. If predicate is not null: The last element in the sequence that passes the test in the specified predicate function. |
143
|
|
|
* @package YaLinqo\Pagination |
144
|
|
|
*/ |
145
|
|
|
public function last ($predicate = null) |
146
|
|
|
{ |
147
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
148
|
|
|
|
149
|
|
|
$found = false; |
150
|
|
|
$value = null; |
151
|
|
|
foreach ($this as $k => $v) { |
152
|
|
|
if ($predicate($v, $k)) { |
153
|
|
|
$found = true; |
154
|
|
|
$value = $v; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
if (!$found) |
158
|
|
|
throw new \UnexpectedValueException(Errors::NO_MATCHES); |
159
|
|
|
return $value; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns the last element of a sequence, or a default value if the sequence contains no elements. |
164
|
|
|
* <p><b>Syntax</b>: lastOrDefault ([default]) |
165
|
|
|
* <p>Returns the last element of a sequence, or a default value if the sequence contains no elements. |
166
|
|
|
* <p><b>Syntax</b>: lastOrDefault ([default [, predicate {(v, k) ==> result}]]) |
167
|
|
|
* <p>Returns the last element of the sequence that satisfies a condition or a default value if no such element is found. |
168
|
|
|
* <p>If obtaining the default value is a costly operation, use {@link lastOrFallback} method to avoid overhead. |
169
|
|
|
* @param mixed $default A default value. |
170
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
171
|
|
|
* @return mixed If predicate is null: default value if source is empty; otherwise, the last element in source. If predicate is not null: default value if source is empty or if no element passes the test specified by predicate; otherwise, the last element in source that passes the test specified by predicate. |
172
|
|
|
* @package YaLinqo\Pagination |
173
|
|
|
*/ |
174
|
|
|
public function lastOrDefault ($default = null, $predicate = null) |
175
|
|
|
{ |
176
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
177
|
|
|
|
178
|
|
|
$found = false; |
179
|
|
|
$value = null; |
180
|
|
|
foreach ($this as $k => $v) { |
181
|
|
|
if ($predicate($v, $k)) { |
182
|
|
|
$found = true; |
183
|
|
|
$value = $v; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
return $found ? $value : $default; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the last element of a sequence, or the result of calling a fallback function if the sequence contains no elements. |
191
|
|
|
* <p><b>Syntax</b>: lastOrFallback ([fallback {() ==> value}]) |
192
|
|
|
* <p>Returns the last element of a sequence, or the result of calling a fallback function if the sequence contains no elements. |
193
|
|
|
* <p><b>Syntax</b>: lastOrFallback ([fallback {() ==> value} [, predicate {(v, k) ==> result}]]) |
194
|
|
|
* <p>Returns the last element of the sequence that satisfies a condition or the result of calling a fallback function if no such element is found. |
195
|
|
|
* <p>The fallback function is not executed if a matching element is found. Use the lastOrFallback method if obtaining the default value is a costly operation to avoid overhead. Otherwise, use {@link lastOrDefault}. |
196
|
|
|
* @param callable $fallback {() ==> value} A fallback function to return the default element. |
197
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
198
|
|
|
* @return mixed If predicate is null: the result of calling a fallback function if source is empty; otherwise, the last element in source. If predicate is not null: the result of calling a fallback function if source is empty or if no element passes the test specified by predicate; otherwise, the last element in source that passes the test specified by predicate. |
199
|
|
|
* @package YaLinqo\Pagination |
200
|
|
|
*/ |
201
|
|
|
public function lastOrFallback ($fallback, $predicate = null) |
202
|
|
|
{ |
203
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
204
|
|
|
|
205
|
|
|
$found = false; |
206
|
|
|
$value = null; |
207
|
|
|
foreach ($this as $k => $v) { |
208
|
|
|
if ($predicate($v, $k)) { |
209
|
|
|
$found = true; |
210
|
|
|
$value = $v; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
return $found ? $value : $fallback(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence. |
218
|
|
|
* <p><b>Syntax</b>: single () |
219
|
|
|
* <p>Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence. |
220
|
|
|
* <p>The single method throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the {@link singleOrDefault} method. |
221
|
|
|
* <p><b>Syntax</b>: single (predicate {(v, k) ==> result}) |
222
|
|
|
* <p>Returns the only element of a sequence that satisfies a specified condition. |
223
|
|
|
* <p>The single method throws an exception if no matching element is found in source. To instead return a default value when no matching element is found, use the {@link singleOrDefault} method. |
224
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
225
|
|
|
* @throws \UnexpectedValueException If source contains no matching elements or more than one matching element. |
226
|
|
|
* @return mixed If predicate is null: the single element of the input sequence. If predicate is not null: The single element of the sequence that passes the test in the specified predicate function. |
227
|
|
|
* @package YaLinqo\Pagination |
228
|
|
|
*/ |
229
|
|
|
public function single ($predicate = null) |
230
|
|
|
{ |
231
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
232
|
|
|
|
233
|
|
|
$found = false; |
234
|
|
|
$value = null; |
235
|
|
|
foreach ($this as $k => $v) { |
236
|
|
|
if ($predicate($v, $k)) { |
237
|
|
|
if ($found) |
238
|
|
|
throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
239
|
|
|
$found = true; |
240
|
|
|
$value = $v; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
if (!$found) |
244
|
|
|
throw new \UnexpectedValueException(Errors::NO_MATCHES); |
245
|
|
|
return $value; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Returns the only element of a sequence, or a default value if the sequence contains no elements. |
250
|
|
|
* <p><b>Syntax</b>: singleOrDefault ([default]) |
251
|
|
|
* <p>Returns the only element of a sequence, or a default value if the sequence contains no elements. |
252
|
|
|
* <p><b>Syntax</b>: singleOrDefault ([default [, predicate {(v, k) ==> result}]]) |
253
|
|
|
* <p>Returns the only element of the sequence that satisfies a condition or a default value if no such element is found. |
254
|
|
|
* <p>If obtaining the default value is a costly operation, use {@link singleOrFallback} method to avoid overhead. |
255
|
|
|
* @param mixed $default A default value. |
256
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
257
|
|
|
* @throws \UnexpectedValueException If source contains more than one matching element. |
258
|
|
|
* @return mixed If predicate is null: default value if source is empty; otherwise, the single element of the source. If predicate is not null: default value if source is empty or if no element passes the test specified by predicate; otherwise, the single element of the source that passes the test specified by predicate. |
259
|
|
|
* @package YaLinqo\Pagination |
260
|
|
|
*/ |
261
|
|
|
public function singleOrDefault ($default = null, $predicate = null) |
262
|
|
|
{ |
263
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
264
|
|
|
|
265
|
|
|
$found = false; |
266
|
|
|
$value = null; |
267
|
|
|
foreach ($this as $k => $v) { |
268
|
|
|
if ($predicate($v, $k)) { |
269
|
|
|
if ($found) |
270
|
|
|
throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
271
|
|
|
$found = true; |
272
|
|
|
$value = $v; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
return $found ? $value : $default; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Returns the only element of a sequence, or the result of calling a fallback function if the sequence contains no elements. |
280
|
|
|
* <p><b>Syntax</b>: singleOrFallback ([fallback {() ==> value}]) |
281
|
|
|
* <p>Returns the only element of a sequence, or the result of calling a fallback function if the sequence contains no elements. |
282
|
|
|
* <p><b>Syntax</b>: singleOrFallback ([fallback {() ==> value} [, predicate {(v, k) ==> result}]]) |
283
|
|
|
* <p>Returns the only element of the sequence that satisfies a condition or the result of calling a fallback function if no such element is found. |
284
|
|
|
* <p>The fallback function is not executed if a matching element is found. Use the singleOrFallback method if obtaining the default value is a costly operation to avoid overhead. Otherwise, use {@link singleOrDefault}. |
285
|
|
|
* @param callable $fallback {() ==> value} A fallback function to return the default element. |
286
|
|
|
* @param callable|null $predicate {(v, k) ==> result} A function to test each element for a condition. Default: true. |
287
|
|
|
* @throws \UnexpectedValueException If source contains more than one matching element. |
288
|
|
|
* @return mixed If predicate is null: the result of calling a fallback function if source is empty; otherwise, the single element of the source. If predicate is not null: the result of calling a fallback function if source is empty or if no element passes the test specified by predicate; otherwise, the single element of the source that passes the test specified by predicate. |
289
|
|
|
* @package YaLinqo\Pagination |
290
|
|
|
*/ |
291
|
|
|
public function singleOrFallback ($fallback, $predicate = null) |
292
|
|
|
{ |
293
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
294
|
|
|
|
295
|
|
|
$found = false; |
296
|
|
|
$value = null; |
297
|
|
|
foreach ($this as $k => $v) { |
298
|
|
|
if ($predicate($v, $k)) { |
299
|
|
|
if ($found) |
300
|
|
|
throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
301
|
|
|
$found = true; |
302
|
|
|
$value = $v; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
return $found ? $value : $fallback(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Searches for the specified value and returns the key of the first occurrence. |
310
|
|
|
* <p><b>Syntax</b>: indexOf (value) |
311
|
|
|
* <p>To search for the zero-based index of the first occurence, call {@link toValues} method first. |
312
|
|
|
* @param mixed $value The value to locate in the sequence. |
313
|
|
|
* @return mixed The key of the first occurrence of value, if found; otherwise, null. |
314
|
|
|
* @package YaLinqo\Pagination |
315
|
|
|
*/ |
316
|
|
|
public function indexOf ($value) |
317
|
|
|
{ |
318
|
|
|
foreach ($this as $k => $v) { |
319
|
|
|
if ($v === $value) |
320
|
|
|
return $k; |
321
|
|
|
} |
322
|
|
|
return null; // not -1 |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Searches for the specified value and returns the key of the last occurrence. |
327
|
|
|
* <p><b>Syntax</b>: lastIndexOf (value) |
328
|
|
|
* <p>To search for the zero-based index of the last occurence, call {@link toValues} method first. |
329
|
|
|
* @param mixed $value The value to locate in the sequence. |
330
|
|
|
* @return mixed The key of the last occurrence of value, if found; otherwise, null. |
331
|
|
|
* @package YaLinqo\Pagination |
332
|
|
|
*/ |
333
|
|
|
public function lastIndexOf ($value) |
334
|
|
|
{ |
335
|
|
|
$key = null; |
336
|
|
|
foreach ($this as $k => $v) { |
337
|
|
|
if ($v === $value) |
338
|
|
|
$key = $k; |
339
|
|
|
} |
340
|
|
|
return $key; // not -1 |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Searches for an element that matches the conditions defined by the specified predicate, and returns the key of the first occurrence. |
345
|
|
|
* <p><b>Syntax</b>: findIndex (predicate {(v, k) ==> result}) |
346
|
|
|
* <p>To search for the zero-based index of the first occurence, call {@link toValues} method first. |
347
|
|
|
* @param callable $predicate {(v, k) ==> result} A function that defines the conditions of the element to search for. |
348
|
|
|
* @return mixed The key of the first occurrence of an element that matches the conditions defined by predicate, if found; otherwise, null. |
349
|
|
|
* @package YaLinqo\Pagination |
350
|
|
|
*/ |
351
|
|
|
public function findIndex ($predicate) |
352
|
|
|
{ |
353
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k'); |
354
|
|
|
|
355
|
|
|
foreach ($this as $k => $v) { |
356
|
|
|
if ($predicate($v, $k)) |
357
|
|
|
return $k; |
358
|
|
|
} |
359
|
|
|
return null; // not -1 |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Searches for an element that matches the conditions defined by the specified predicate, and returns the key of the last occurrence. |
364
|
|
|
* <p><b>Syntax</b>: findLastIndex (predicate {(v, k) ==> result}) |
365
|
|
|
* <p>To search for the zero-based index of the last occurence, call {@link toValues} method first. |
366
|
|
|
* @param callable $predicate {(v, k) ==> result} A function that defines the conditions of the element to search for. |
367
|
|
|
* @return mixed The key of the last occurrence of an element that matches the conditions defined by predicate, if found; otherwise, null. |
368
|
|
|
* @package YaLinqo\Pagination |
369
|
|
|
*/ |
370
|
|
|
public function findLastIndex ($predicate) |
371
|
|
|
{ |
372
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k'); |
373
|
|
|
|
374
|
|
|
$key = null; |
375
|
|
|
foreach ($this as $k => $v) { |
376
|
|
|
if ($predicate($v, $k)) |
377
|
|
|
$key = $k; |
378
|
|
|
} |
379
|
|
|
return $key; // not -1 |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Bypasses a specified number of elements in a sequence and then returns the remaining elements. |
384
|
|
|
* <p><b>Syntax</b>: skip (count) |
385
|
|
|
* <p>If source contains fewer than count elements, an empty sequence is returned. If count is less than or equal to zero, all elements of source are yielded. |
386
|
|
|
* <p>The {@link take} and skip methods are functional complements. Given a sequence coll and an integer n, concatenating the results of coll->take(n) and coll->skip(n) yields the same sequence as coll. |
387
|
|
|
* @param int $count The number of elements to skip before returning the remaining elements. |
388
|
|
|
* @return Enumerable A sequence that contains the elements that occur after the specified index in the input sequence. |
389
|
|
|
* @package YaLinqo\Pagination |
390
|
|
|
*/ |
391
|
|
|
public function skip ($count) |
392
|
|
|
{ |
393
|
|
|
return new self(function () use ($count) { |
394
|
|
|
$it = $this->getIterator(); |
395
|
|
|
$it->rewind(); |
396
|
|
|
for ($i = 0; $i < $count && $it->valid(); ++$i) |
397
|
|
|
$it->next(); |
398
|
|
|
while ($it->valid()) { |
399
|
|
|
yield $it->key() => $it->current(); |
400
|
|
|
$it->next(); |
401
|
|
|
} |
402
|
|
|
}); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. |
407
|
|
|
* <p><b>Syntax</b>: skipWhile (predicate {(v, k) ==> result}) |
408
|
|
|
* <p>This method tests each element of source by using predicate and skips the element if the result is true. After the predicate function returns false for an element, that element and the remaining elements in source are yielded and there are no more invocations of predicate. If predicate returns true for all elements in the sequence, an empty sequence is returned. |
409
|
|
|
* <p>The {@link takeWhile} and skipWhile methods are functional complements. Given a sequence coll and a pure function p, concatenating the results of coll->takeWhile(p) and coll->skipWhile(p) yields the same sequence as coll. |
410
|
|
|
* @param callable $predicate {(v, k) ==> result} A function to test each element for a condition. |
411
|
|
|
* @return Enumerable A sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate. |
412
|
|
|
* @package YaLinqo\Pagination |
413
|
|
|
*/ |
414
|
|
|
public function skipWhile ($predicate) |
415
|
|
|
{ |
416
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k'); |
417
|
|
|
|
418
|
|
|
return new self(function () use ($predicate) { |
419
|
|
|
$yielding = false; |
420
|
|
|
foreach ($this as $k => $v) { |
421
|
|
|
if (!$yielding && !$predicate($v, $k)) |
422
|
|
|
$yielding = true; |
423
|
|
|
if ($yielding) |
424
|
|
|
yield $k => $v; |
425
|
|
|
} |
426
|
|
|
}); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Returns a specified number of contiguous elements from the start of a sequence. |
431
|
|
|
* <p><b>Syntax</b>: take (count) |
432
|
|
|
* <p>Take enumerates source and yields elements until count elements have been yielded or source contains no more elements. If count is less than or equal to zero, source is not enumerated and an empty sequence is returned. |
433
|
|
|
* <p>The take and {@link skip} methods are functional complements. Given a sequence coll and an integer n, concatenating the results of coll->take(n) and coll->skip(n) yields the same sequence as coll. |
434
|
|
|
* @param int $count The number of elements to return. |
435
|
|
|
* @return Enumerable A sequence that contains the specified number of elements from the start of the input sequence. |
436
|
|
|
* @package YaLinqo\Pagination |
437
|
|
|
*/ |
438
|
|
|
public function take ($count) |
439
|
|
|
{ |
440
|
|
|
if ($count <= 0) |
441
|
|
|
return new self(new \EmptyIterator, false); |
442
|
|
|
|
443
|
|
|
return new self(function () use ($count) { |
444
|
|
|
foreach ($this as $k => $v) { |
445
|
|
|
yield $k => $v; |
446
|
|
|
if (--$count == 0) |
447
|
|
|
break; |
448
|
|
|
} |
449
|
|
|
}); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Returns elements from a sequence as long as a specified condition is true. |
454
|
|
|
* <p><b>Syntax</b>: takeWhile (predicate {(v, k) ==> result}) |
455
|
|
|
* <p>The takeWhile method tests each element of source by using predicate and yields the element if the result is true. Enumeration stops when the predicate function returns false for an element or when source contains no more elements. |
456
|
|
|
* <p>The takeWhile and {@link skipWhile} methods are functional complements. Given a sequence coll and a pure function p, concatenating the results of coll->takeWhile(p) and coll->skipWhile(p) yields the same sequence as coll. |
457
|
|
|
* @param callable $predicate {(v, k) ==> result} A function to test each element for a condition. |
458
|
|
|
* @return Enumerable A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes. |
459
|
|
|
* @package YaLinqo\Pagination |
460
|
|
|
*/ |
461
|
|
|
public function takeWhile ($predicate) |
462
|
|
|
{ |
463
|
|
|
$predicate = Utils::createLambda($predicate, 'v,k'); |
464
|
|
|
|
465
|
|
|
return new self(function () use ($predicate) { |
466
|
|
|
foreach ($this as $k => $v) { |
467
|
|
|
if (!$predicate($v, $k)) |
468
|
|
|
break; |
469
|
|
|
yield $k => $v; |
470
|
|
|
} |
471
|
|
|
}); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @return \Iterator |
476
|
|
|
*/ |
477
|
|
|
public abstract function getIterator (); |
478
|
|
|
} |