This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ITraversable.php
326 lines (289 loc) · 8.53 KB
/
ITraversable.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
namespace Pinq;
use Pinq\Interfaces\IJoiningOnTraversable;
use Pinq\Interfaces\IOrderedTraversable;
use Pinq\Iterators\IIteratorScheme;
/**
* The root interface providing a fluent query API for a range of key value pairs.
* Query calls must be immutable and return a new instance with every query call.
* Queries should also be executed lazily upon iteration.
*
* @author Elliot Levin <elliotlevin@hotmail.com>
*/
interface ITraversable extends IAggregatable, \IteratorAggregate, \ArrayAccess
{
const ITRAVERSABLE_TYPE = __CLASS__;
/**
* Returns the values as an array.
* Only valid array keys (strings or integers) will be used,
* all others will be reindexed numerically.
*
* @return array
*/
public function asArray();
/**
* Returns the values as a traversable.
* The following queries will be performed in memory.
*
* @return ITraversable
*/
public function asTraversable();
/**
* Returns the values as a collection.
* The following queries will be performed in memory.
*
* @return ICollection
*/
public function asCollection();
/**
* Returns whether the traversable is the source of the elements.
*
* @return boolean
*/
public function isSource();
/**
* Returns the source traversable or itself if the current traversable
* is the source.
*
* @return ITraversable
*/
public function getSource();
/**
* Iterates the elements with the supplied function.
* Returning false will break the iteration loop.
*
* @param callable $function The iteration function, parameters are passed as ($value, $key)
*
* @return void
*/
public function iterate(callable $function);
/**
* Returns an array compatible iterator for the elements.
* Non string or integer keys will be numerically reindexed.
*
* @return \Traversable
*/
public function getIterator(): \Traversable;
/**
* Returns an iterator for all the elements.
* All keys types will remain unaltered.
*
* @return \Traversable
*/
public function getTrueIterator();
/**
* Returns the underlying iterator scheme used by the implementation.
*
* @return IIteratorScheme
*/
public function getIteratorScheme();
/**
* Returns the first value, null if empty
*
* @return mixed The first value
*/
public function first();
/**
* Returns the last value, null if empty
*
* @return mixed The last value
*/
public function last();
/**
* Returns whether the supplied value is contained within the aggregate
*
* @param mixed $value The value to check for
*
* @return boolean
*/
public function contains($value);
/**
* Filters the values by a supplied predicate.
*
* @param callable $predicate The predicate function
*
* @return ITraversable
*/
public function where(callable $predicate);
/**
* Orders the values mapped from the supplied function according the supplied
* direction.
*
* @param callable $function The projection function
* @param int $direction
*
* @return IOrderedTraversable
*/
public function orderBy(callable $function, $direction);
/**
* Orders the values mapped from the supplied function ascendingly
* Example function:
*
* @param callable $function The mapping function
*
* @return IOrderedTraversable
*/
public function orderByAscending(callable $function);
/**
* Orders the values mapped from the supplied function descendingly
* Example expression function:
*
* @param callable $function The mapping function
*
* @return IOrderedTraversable
*/
public function orderByDescending(callable $function);
/**
* Skip the amount of values from the start.
*
* @param int $amount The amount of values to skip, must be > 0
*
* @return ITraversable
*/
public function skip($amount);
/**
* Limits the amount of values by the supplied amount
*
* @param int|null $amount The amount of values to retrieve, must be > 0 or null if all
*
* @return ITraversable
*/
public function take($amount);
/**
* Retrieve a slice of the values.
*
* @param int $start The amount of values to skip
* @param int|null $amount The amount of values to retrieve
*
* @return ITraversable
*/
public function slice($start, $amount);
/**
* Index the values according to the supplied mapping function.
* All duplicate indexes will be associated with the first value
* for that index.
*
* @param callable $function The projection function
*
* @return ITraversable
*/
public function indexBy(callable $function);
/**
* Selects the keys as the values (indexed by their 0-based position).
*
* @return ITraversable
*/
public function keys();
/**
* Indexes the values by their 0-based position.
*
* @return ITraversable
*/
public function reindex();
/**
* Groups values according the supplied function. (Uses strict equality '===')
* The values will be grouped into instances of the traversable.
* This will implicitly index each group by the group key returned from the supplied function.
*
* @param callable $function The grouping function
*
* @return ITraversable
*/
public function groupBy(callable $function);
/**
* Matches the values with the supplied values according to the supplied filter
* then maps the results into as according to the supplied function.
*
* @param array|\Traversable $values
*
* @return IJoiningOnTraversable
*/
public function join($values);
/**
* Matches the values with the supplied values according to the supplied filter,
* groups the the joined values for every original and then maps into as
* according to the supplied function.
*
* @param array|\Traversable $values
*
* @return IJoiningOnTraversable
*/
public function groupJoin($values);
/**
* Returns the values mapped by the supplied function.
*
* @param callable $function The function returning the data to select
*
* @return ITraversable
*/
public function select(callable $function);
/**
* Returns the values mapped by the supplied function and then flattens
* the values into a single traversable. Keys will be reindexed.
*
* @param callable $function The function returning the data to select
*
* @return ITraversable
*/
public function selectMany(callable $function);
/**
* Only return unique values. (Uses strict equality '===')
*
* @return ITraversable
*/
public function unique();
/**
* Returns values from the original and supplied values, keys will be reindexed.
*
* @param array|\Traversable $values The values to append
*
* @return ITraversable
*/
public function append($values);
/**
* Returns all values from the original present in the supplied values.
* (Uses strict equality '===')
*
* @param array|\Traversable $values
*
* @return ITraversable
*/
public function whereIn($values);
/**
* Returns values all values from the original not present in the supplied values.
* (Uses strict equality '===')
*
* @param array|\Traversable $values The values to union
*
* @return ITraversable
*/
public function except($values);
/**
* Returns unique results present in both the original and supplied values,
* keys will be reindexed.
* (Uses strict equality '===')
*
* @param array|\Traversable $values The values to union
*
* @return ITraversable
*/
public function union($values);
/**
* Returns unique values the are present in the original and supplied values.
* (Uses strict equality '===')
*
* @param array|\Traversable $values The values to intersect with
*
* @return ITraversable
*/
public function intersect($values);
/**
* Returns unique values from the original not present in the supplied values.
* (Uses strict equality '===')
*
* @param array|\Traversable $values The values to remove
*
* @return ITraversable
*/
public function difference($values);
}