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
|
48 |
|
public function __construct($items = []) |
16
|
|
|
{ |
17
|
48 |
|
parent::__construct($items); |
18
|
48 |
|
Assert::allIsInstanceOf($this->items, $this->type); |
19
|
44 |
|
} |
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 |
|
return $this->toBase()->mapToDictionary($callback); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Run a grouping map over the items. |
141
|
|
|
* |
142
|
|
|
* The callback should return an associative array with a single key/value pair. |
143
|
|
|
* |
144
|
|
|
* @param callable $callback |
145
|
|
|
* @return Collection |
146
|
|
|
*/ |
147
|
1 |
|
public function mapToGroups(callable $callback) |
148
|
|
|
{ |
149
|
1 |
|
return $this->toBase()->mapToGroups($callback); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Run an associative map over each of the items. |
154
|
|
|
* |
155
|
|
|
* The callback should return an associative array with a single key/value pair. |
156
|
|
|
* |
157
|
|
|
* @param callable $callback |
158
|
|
|
* @return Collection |
159
|
|
|
*/ |
160
|
1 |
|
public function mapWithKeys(callable $callback) |
161
|
|
|
{ |
162
|
1 |
|
return $this->toBase()->mapWithKeys($callback); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Map the values into a new class. |
167
|
|
|
* |
168
|
|
|
* @param string $class |
169
|
|
|
* @return Collection |
170
|
|
|
*/ |
171
|
1 |
|
public function mapInto($class) |
172
|
|
|
{ |
173
|
1 |
|
return $this->toBase()->mapInto($class); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Create a collection by using this collection for keys and another for its values. |
178
|
|
|
* |
179
|
|
|
* @param mixed $values |
180
|
|
|
* @throws InvalidOperationException |
181
|
|
|
*/ |
182
|
1 |
|
public function combine($values) |
183
|
|
|
{ |
184
|
1 |
|
throw new InvalidOperationException('Flip() can never work on a TypeSafeCollection'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Partition the collection into two arrays using the given callback or key. |
189
|
|
|
* |
190
|
|
|
* @param callable|string $key |
191
|
|
|
* @param mixed $operator |
192
|
|
|
* @param mixed $value |
193
|
|
|
* @return Collection |
194
|
|
|
*/ |
195
|
1 |
|
public function partition($key, $operator = null, $value = null) |
196
|
|
|
{ |
197
|
1 |
|
$partitions = [new static(), new static()]; |
198
|
|
|
|
199
|
1 |
|
$callback = \func_num_args() === 1 |
200
|
1 |
|
? $this->valueRetriever($key) |
201
|
1 |
|
: $this->operatorForWhere(...\func_get_args()); |
|
|
|
|
202
|
|
|
|
203
|
1 |
|
foreach ($this->items as $key => $item) { |
|
|
|
|
204
|
1 |
|
$partitions[(int) !$callback($item, $key)][$key] = $item; |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
return new Collection($partitions); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Group an associative array by a field or using a callback. |
212
|
|
|
* |
213
|
|
|
* @param callable|string $groupBy |
214
|
|
|
* @param bool $preserveKeys |
215
|
|
|
* @return Collection |
216
|
|
|
*/ |
217
|
2 |
|
public function groupBy($groupBy, $preserveKeys = false) |
218
|
|
|
{ |
219
|
2 |
|
return $this->toBase()->groupBy($groupBy, $preserveKeys); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the keys of the collection items. |
224
|
|
|
* |
225
|
|
|
* @return Collection |
226
|
|
|
*/ |
227
|
1 |
|
public function keys() |
228
|
|
|
{ |
229
|
1 |
|
return $this->toBase()->keys(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set the item at a given offset. |
234
|
|
|
* |
235
|
|
|
* @param mixed $key |
236
|
|
|
* @param mixed $value |
237
|
|
|
*/ |
238
|
5 |
|
public function offsetSet($key, $value): void |
239
|
|
|
{ |
240
|
5 |
|
Assert::isInstanceOf($value, $this->type); |
241
|
|
|
|
242
|
3 |
|
parent::offsetSet($key, $value); |
243
|
3 |
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Zip the collection together with one or more arrays. |
247
|
|
|
* |
248
|
|
|
* e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
249
|
|
|
* => [[1, 4], [2, 5], [3, 6]] |
250
|
|
|
* |
251
|
|
|
* @param mixed ...$items |
252
|
|
|
* @return Collection |
253
|
|
|
*/ |
254
|
1 |
|
public function zip($items) |
255
|
|
|
{ |
256
|
|
|
$arrayableItems = array_map(function ($items) { |
257
|
1 |
|
return $this->getArrayableItems($items); |
258
|
1 |
|
}, \func_get_args()); |
259
|
|
|
|
260
|
|
|
$params = array_merge([function () { |
261
|
1 |
|
return new static(\func_get_args()); |
262
|
1 |
|
}, $this->items], $arrayableItems); |
263
|
|
|
|
264
|
1 |
|
return new Collection(\call_user_func_array('array_map', $params)); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Count the number of items in the collection using a given truth test. |
269
|
|
|
* |
270
|
|
|
* @param callable|null $callback |
271
|
|
|
* @return Collection |
272
|
|
|
*/ |
273
|
1 |
|
public function countBy($callback = null) |
274
|
|
|
{ |
275
|
1 |
|
return $this->toBase()->countBy($callback); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Add an item to the collection. |
280
|
|
|
* |
281
|
|
|
* @param mixed $item |
282
|
|
|
* @return $this |
283
|
|
|
*/ |
284
|
2 |
|
public function add($item) |
285
|
|
|
{ |
286
|
2 |
|
Assert::isInstanceOf($item, $this->type); |
287
|
|
|
|
288
|
1 |
|
$this->items[] = $item; |
289
|
|
|
|
290
|
1 |
|
return $this; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|