/
Collection.php
450 lines (396 loc) · 9.75 KB
/
Collection.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
namespace Limelight\Classes;
use ArrayAccess;
use Limelight\Helpers\Arr;
use Limelight\Classes\LimelightResults;
abstract class Collection implements ArrayAccess
{
use Arr;
/**
* Collection methods adopted from Laravel Collection.
* https://github.com/illuminate/support/blob/master/Collection.php
*/
/**
* Get all words.
*
* @return $this
*/
public function all()
{
return $this->words;
}
/**
* Chunk the underlying collection array.
*
* @param int $size
*
* @return static
*/
public function chunk($size)
{
$chunks = [];
foreach (array_chunk($this->words, $size, true) as $chunk) {
$chunks[] = new static($this->text, $chunk, $this->pluginData);
}
return new static($this->text, $chunks, $this->pluginData);
}
/**
* Count the number of items on the object.
*
* @return int
*/
public function count()
{
return count($this->words);
}
/**
* Get all items except for those with the specified keys.
*
* @param mixed $keys
*
* @return static
*/
public function except($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return new static($this->text, $this->arrExcept($this->words, $keys), $this->pluginData);
}
/**
* Run a filter over each of the items.
*
* @param callable|null $callback
*
* @return static
*/
public function filter(callable $callback = null)
{
if ($callback) {
$return = [];
foreach ($this->words as $key => $value) {
if ($callback($value, $key)) {
$return[$key] = $value;
}
}
return new static($this->text, $return, $this->pluginData);
}
return new static($this->text, array_filter($this->words), $this->pluginData);
}
/**
* Get the first item from the collection.
*
* @param callable|null $callback
* @param mixed $default
*
* @return mixed
*/
public function first(callable $callback = null, $default = null)
{
return $this->arrFirst($this->words, $callback, $default);
}
/**
* Remove an item from the collection by key.
*
* @param string|array $keys
*
* @return $this
*/
public function forget($keys)
{
foreach ((array) $keys as $key) {
$this->offsetUnset($key);
}
return $this;
}
/**
* Concatenate values of a given key as a string.
*
* @param string $value
* @param string $glue
*
* @return string
*/
public function implode($value, $glue = null)
{
$first = $this->first();
if (is_array($first) || is_object($first)) {
return implode($glue, $this->pluck($value)->all());
}
return implode($value, $this->words);
}
/**
* Determine if the collection is empty or not.
*
* @return bool
*/
public function isEmpty()
{
return empty($this->words);
}
/**
* Get the last item from the collection.
*
* @param callable|null $callback
* @param mixed $default
*
* @return mixed
*/
public function last(callable $callback = null, $default = null)
{
return $this->arrLast($this->words, $callback, $default);
}
/**
* Run a map over each of the items.
*
* @param callable $callback
*
* @return static
*/
public function map(callable $callback)
{
$keys = array_keys($this->words);
$items = array_map($callback, $this->words, $keys);
return new static($this->text, array_combine($keys, $items), $this->pluginData);
}
/**
* Merge the collection with the given items.
*
* @param mixed $items
*
* @return static
*/
public function merge($items)
{
return new static($this->text, array_merge($this->words, $this->getArrayableItems($items)), $this->pluginData);
}
/**
* Get an item at a given offset.
*
* @param mixed $key
*
* @return mixed
*/
public function offsetGet($key)
{
return $this->words[$key];
}
/**
* Determine if an item exists at an offset.
*
* @param mixed $key
*
* @return bool
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->words);
}
/**
* Set the item at a given offset.
*
* @param mixed $key
* @param mixed $value
*
* @return void
*/
public function offsetSet($key, $value)
{
if (is_null($key)) {
$this->words[] = $value;
} else {
$this->words[$key] = $value;
}
}
/**
* Unset the item at a given offset.
*
* @param string $key
*
* @return void
*/
public function offsetUnset($key)
{
unset($this->words[$key]);
}
/**
* Get the items with the specified keys.
*
* @param mixed $keys
*
* @return static
*/
public function only($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return new static($this->text, $this->arrOnly($this->words, $keys), $this->pluginData);
}
/**
* Get the values of a given key.
*
* @param string $value
* @param string|null $key
*
* @return static
*/
public function pluck($value, $key = null)
{
return new static($this->text, $this->arrPluck($this->words, $value, $key), $this->pluginData);
}
/**
* Get and remove the last item from the collection.
*
* @return mixed
*/
public function pop()
{
return array_pop($this->words);
}
/**
* Push an item onto the beginning of the collection.
*
* @param mixed $value
* @param mixed $key
*
* @return $this
*/
public function prepend($value, $key = null)
{
$this->words = $this->arrPrepend($this->words, $value, $key);
return $this;
}
/**
* Get and remove an item from the collection.
*
* @param mixed $key
* @param mixed $default
*
* @return mixed
*/
public function pull($key, $default = null)
{
return $this->arrPull($this->words, $key, $default);
}
/**
* Push an item onto the end of the collection.
*
* @param mixed $value
*
* @return $this
*/
public function push($value)
{
$this->offsetSet(null, $value);
return $this;
}
/**
* Create a collection of all elements that do not pass a given truth test.
*
* @param callable|mixed $callback
*
* @return static
*/
public function reject($callback)
{
if ($this->useAsCallable($callback)) {
return $this->filter(function ($value, $key) use ($callback) {
return ! $callback($value, $key);
});
}
return $this->filter(function ($item) use ($callback) {
return $item != $callback;
});
}
/**
* Get and remove the first item from the collection.
*
* @return mixed
*/
public function shift()
{
return array_shift($this->words);
}
/**
* Slice the underlying collection array.
*
* @param int $offset
* @param int $length
*
* @return static
*/
public function slice($offset, $length = null)
{
return new static($this->text, array_slice($this->words, $offset, $length, true), $this->pluginData);
}
/**
* Splice a portion of the underlying collection array.
*
* @param int $offset
* @param int|null $length
* @param mixed $replacement
*
* @return static
*/
public function splice($offset, $length = null, $replacement = [])
{
if (func_num_args() == 1) {
return new static($this->text, array_splice($this->words, $offset), $this->pluginData);
}
return new static($this->text, array_splice($this->words, $offset, $length, $replacement), $this->pluginData);
}
/**
* Take the first or last {$limit} items.
*
* @param int $limit
*
* @return static
*/
public function take($limit)
{
if ($limit < 0) {
return $this->slice($limit, abs($limit));
}
return $this->slice(0, $limit);
}
/**
* Get the collection of items as a plain array.
*
* @return array
*/
public function toArray()
{
return array_map(function ($value) {
return $value instanceof Arrayable ? $value->toArray() : $value;
}, $this->words);
}
/**
* Transform each item in the collection using a callback.
*
* @param callable $callback
*
* @return $this
*/
public function transform(callable $callback)
{
$this->words = $this->map($callback)->all();
return $this;
}
/**
* Return only unique items from the collection array.
*
* @param string|callable|null $key
* @return static
*/
public function unique($key = null)
{
if (is_null($key)) {
return new static($this->text, array_unique($this->words, SORT_REGULAR), $this->pluginData);
}
$key = $this->valueRetriever($key);
$exists = [];
return $this->reject(function ($item) use ($key, &$exists) {
if (in_array($id = $key($item), $exists)) {
return true;
}
$exists[] = $id;
});
}
}