|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webparking\TypeSafeCollection\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Webmozart\Assert\Assert; |
|
9
|
|
|
use Webparking\TypeSafeCollection\Exceptions\InvalidOperationException; |
|
10
|
|
|
|
|
11
|
|
|
abstract class TypeSafeCollection extends EloquentCollection |
|
12
|
|
|
{ |
|
13
|
|
|
protected $type; |
|
14
|
|
|
|
|
15
|
45 |
|
public function __construct($items = []) |
|
16
|
|
|
{ |
|
17
|
45 |
|
parent::__construct($items); |
|
18
|
45 |
|
Assert::allIsInstanceOf($this->items, $this->type); |
|
19
|
41 |
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a new collection by invoking the callback a given amount of times. |
|
23
|
|
|
* |
|
24
|
|
|
* @param int $number |
|
25
|
|
|
* @param callable $callback |
|
26
|
|
|
* @return Collection |
|
27
|
|
|
*/ |
|
28
|
2 |
|
public static function times($number, callable $callback = null) |
|
29
|
|
|
{ |
|
30
|
2 |
|
return new static(Collection::times($number, $callback)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Collapse the collection of items into a single array. |
|
35
|
|
|
* |
|
36
|
|
|
* @return static |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function collapse() |
|
39
|
|
|
{ |
|
40
|
1 |
|
return new static(Arr::collapse($this->items)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Cross join with the given lists, returning all possible permutations. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed ...$lists |
|
47
|
|
|
* @return Collection |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function crossJoin(...$lists) |
|
50
|
|
|
{ |
|
51
|
1 |
|
return new Collection(Arr::crossJoin( |
|
52
|
1 |
|
$this->items, ...array_map([$this, 'getArrayableItems'], $lists) |
|
53
|
|
|
)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Push an item onto the beginning of the collection. |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $value |
|
60
|
|
|
* @param mixed $key |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function prepend($value, $key = null) |
|
64
|
|
|
{ |
|
65
|
2 |
|
Assert::isInstanceOf($value, $this->type); |
|
66
|
|
|
|
|
67
|
1 |
|
parent::prepend($value, $key); |
|
68
|
|
|
|
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Chunk the underlying collection array. |
|
74
|
|
|
* |
|
75
|
|
|
* @param int $size |
|
76
|
|
|
* @return EloquentCollection |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function chunk($size) |
|
79
|
|
|
{ |
|
80
|
2 |
|
if ($size <= 0) { |
|
81
|
1 |
|
return new EloquentCollection(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
$chunks = []; |
|
85
|
|
|
|
|
86
|
1 |
|
foreach (array_chunk($this->items, $size, true) as $chunk) { |
|
87
|
1 |
|
$chunks[] = new static($chunk); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return new EloquentCollection($chunks); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Dump the collection. |
|
95
|
|
|
* |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
|
|
public function dump() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->toBase()->dump(); |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Flip the items in the collection. |
|
107
|
|
|
* |
|
108
|
|
|
* @throws InvalidOperationException |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function flip() |
|
111
|
|
|
{ |
|
112
|
1 |
|
throw new InvalidOperationException('Flip() can never work on a TypeSafeCollection'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Run a map over each of the items. |
|
117
|
|
|
* |
|
118
|
|
|
* @param callable $callback |
|
119
|
|
|
* @return Collection |
|
120
|
|
|
*/ |
|
121
|
3 |
|
public function map(callable $callback) |
|
122
|
|
|
{ |
|
123
|
3 |
|
return $this->toBase()->map($callback); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Run a dictionary map over the items. |
|
128
|
|
|
* |
|
129
|
|
|
* The callback should return an associative array with a single key/value pair. |
|
130
|
|
|
* |
|
131
|
|
|
* @param callable $callback |
|
132
|
|
|
* @return Collection |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function mapToDictionary(callable $callback) |
|
135
|
|
|
{ |
|
136
|
1 |
|
$base = $this->toBase(); |
|
137
|
|
|
|
|
138
|
1 |
|
if (method_exists($base, 'mapToDictionary')) { |
|
139
|
1 |
|
return $base->mapToDictionary($callback); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
throw new InvalidOperationException('This method is not implemented in the Collection you\'re using'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Run a grouping map over the items. |
|
147
|
|
|
* |
|
148
|
|
|
* The callback should return an associative array with a single key/value pair. |
|
149
|
|
|
* |
|
150
|
|
|
* @param callable $callback |
|
151
|
|
|
* @return Collection |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function mapToGroups(callable $callback) |
|
154
|
|
|
{ |
|
155
|
1 |
|
return $this->toBase()->mapToGroups($callback); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Run an associative map over each of the items. |
|
160
|
|
|
* |
|
161
|
|
|
* The callback should return an associative array with a single key/value pair. |
|
162
|
|
|
* |
|
163
|
|
|
* @param callable $callback |
|
164
|
|
|
* @return Collection |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function mapWithKeys(callable $callback) |
|
167
|
|
|
{ |
|
168
|
1 |
|
return $this->toBase()->mapWithKeys($callback); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Map the values into a new class. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $class |
|
175
|
|
|
* @return Collection |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public function mapInto($class) |
|
178
|
|
|
{ |
|
179
|
1 |
|
return $this->toBase()->mapInto($class); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Create a collection by using this collection for keys and another for its values. |
|
184
|
|
|
* |
|
185
|
|
|
* @param mixed $values |
|
186
|
|
|
* @throws InvalidOperationException |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function combine($values) |
|
189
|
|
|
{ |
|
190
|
1 |
|
throw new InvalidOperationException('Flip() can never work on a TypeSafeCollection'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Partition the collection into two arrays using the given callback or key. |
|
195
|
|
|
* |
|
196
|
|
|
* @param callable|string $key |
|
197
|
|
|
* @param mixed $operator |
|
198
|
|
|
* @param mixed $value |
|
199
|
|
|
* @return Collection |
|
200
|
|
|
*/ |
|
201
|
1 |
|
public function partition($key, $operator = null, $value = null) |
|
202
|
|
|
{ |
|
203
|
1 |
|
$partitions = [new static(), new static()]; |
|
204
|
|
|
|
|
205
|
1 |
|
$callback = \func_num_args() === 1 |
|
206
|
1 |
|
? $this->valueRetriever($key) |
|
207
|
1 |
|
: $this->operatorForWhere(...\func_get_args()); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
1 |
|
foreach ($this->items as $key => $item) { |
|
|
|
|
|
|
210
|
1 |
|
$partitions[(int) !$callback($item, $key)][$key] = $item; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
1 |
|
return new Collection($partitions); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Group an associative array by a field or using a callback. |
|
218
|
|
|
* |
|
219
|
|
|
* @param callable|string $groupBy |
|
220
|
|
|
* @param bool $preserveKeys |
|
221
|
|
|
* @return Collection |
|
222
|
|
|
*/ |
|
223
|
2 |
|
public function groupBy($groupBy, $preserveKeys = false) |
|
224
|
|
|
{ |
|
225
|
2 |
|
return $this->toBase()->groupBy($groupBy, $preserveKeys); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get the keys of the collection items. |
|
230
|
|
|
* |
|
231
|
|
|
* @return Collection |
|
232
|
|
|
*/ |
|
233
|
1 |
|
public function keys() |
|
234
|
|
|
{ |
|
235
|
1 |
|
return $this->toBase()->keys(); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Set the item at a given offset. |
|
240
|
|
|
* |
|
241
|
|
|
* @param mixed $key |
|
242
|
|
|
* @param mixed $value |
|
243
|
|
|
*/ |
|
244
|
5 |
|
public function offsetSet($key, $value): void |
|
245
|
|
|
{ |
|
246
|
5 |
|
Assert::isInstanceOf($value, $this->type); |
|
247
|
|
|
|
|
248
|
3 |
|
parent::offsetSet($key, $value); |
|
249
|
3 |
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Zip the collection together with one or more arrays. |
|
253
|
|
|
* |
|
254
|
|
|
* e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
|
255
|
|
|
* => [[1, 4], [2, 5], [3, 6]] |
|
256
|
|
|
* |
|
257
|
|
|
* @param mixed ...$items |
|
258
|
|
|
* @return Collection |
|
259
|
|
|
*/ |
|
260
|
1 |
|
public function zip($items) |
|
261
|
|
|
{ |
|
262
|
|
|
$arrayableItems = array_map(function ($items) { |
|
263
|
1 |
|
return $this->getArrayableItems($items); |
|
264
|
1 |
|
}, \func_get_args()); |
|
265
|
|
|
|
|
266
|
|
|
$params = array_merge([function () { |
|
267
|
1 |
|
return new static(\func_get_args()); |
|
268
|
1 |
|
}, $this->items], $arrayableItems); |
|
269
|
|
|
|
|
270
|
1 |
|
return new Collection(\call_user_func_array('array_map', $params)); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Count the number of items in the collection using a given truth test. |
|
275
|
|
|
* |
|
276
|
|
|
* @param callable|null $callback |
|
277
|
|
|
* @return Collection |
|
278
|
|
|
*/ |
|
279
|
1 |
|
public function countBy($callback = null) |
|
280
|
|
|
{ |
|
281
|
1 |
|
$base = $this->toBase(); |
|
282
|
|
|
|
|
283
|
1 |
|
if (method_exists($base, 'countBy')) { |
|
284
|
1 |
|
return $base->countBy($callback); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
throw new InvalidOperationException('This method is not implemented in the Collection you\'re using'); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Add an item to the collection. |
|
292
|
|
|
* |
|
293
|
|
|
* @param mixed $item |
|
294
|
|
|
* @return $this |
|
295
|
|
|
*/ |
|
296
|
2 |
|
public function add($item) |
|
297
|
|
|
{ |
|
298
|
2 |
|
Assert::isInstanceOf($item, $this->type); |
|
299
|
|
|
|
|
300
|
1 |
|
$this->items[] = $item; |
|
301
|
|
|
|
|
302
|
1 |
|
return $this; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|