|
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
|
|
|
|
|
10
|
|
|
abstract class TypeSafeCollection extends EloquentCollection |
|
11
|
|
|
{ |
|
12
|
|
|
protected $type; |
|
13
|
|
|
|
|
14
|
17 |
|
public function __construct($items = []) |
|
15
|
|
|
{ |
|
16
|
17 |
|
parent::__construct($items); |
|
17
|
17 |
|
Assert::allIsInstanceOf($this->items, $this->type); |
|
18
|
13 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new collection by invoking the callback a given amount of times. |
|
22
|
|
|
* |
|
23
|
|
|
* @param int $number |
|
24
|
|
|
* @param callable $callback |
|
25
|
|
|
* @return Collection |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public static function times($number, callable $callback = null) |
|
28
|
|
|
{ |
|
29
|
1 |
|
return new static(Collection::times($number, $callback)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Collapse the collection of items into a single array. |
|
34
|
|
|
* |
|
35
|
|
|
* @return static |
|
36
|
|
|
*/ |
|
37
|
|
|
public function collapse() |
|
38
|
|
|
{ |
|
39
|
|
|
return new static(Arr::collapse($this->items)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Cross join with the given lists, returning all possible permutations. |
|
44
|
|
|
* |
|
45
|
|
|
* @param mixed ...$lists |
|
46
|
|
|
* @return Collection |
|
47
|
|
|
*/ |
|
48
|
|
|
public function crossJoin(...$lists) |
|
49
|
|
|
{ |
|
50
|
|
|
return new Collection(Arr::crossJoin( |
|
51
|
|
|
$this->items, ...array_map([$this, 'getArrayableItems'], $lists) |
|
52
|
|
|
)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Push an item onto the beginning of the collection. |
|
57
|
|
|
* |
|
58
|
|
|
* @param mixed $value |
|
59
|
|
|
* @param mixed $key |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function prepend($value, $key = null) |
|
63
|
|
|
{ |
|
64
|
2 |
|
Assert::isInstanceOf($value, $this->type); |
|
65
|
|
|
|
|
66
|
1 |
|
parent::prepend($value, $key); |
|
67
|
|
|
|
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Chunk the underlying collection array. |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $size |
|
75
|
|
|
* @return EloquentCollection |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function chunk($size) |
|
78
|
|
|
{ |
|
79
|
1 |
|
if ($size <= 0) { |
|
80
|
|
|
return new EloquentCollection(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
$chunks = []; |
|
84
|
|
|
|
|
85
|
1 |
|
foreach (array_chunk($this->items, $size, true) as $chunk) { |
|
86
|
1 |
|
$chunks[] = new static($chunk); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return new EloquentCollection($chunks); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Dump the collection. |
|
94
|
|
|
* |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
|
|
public function dump() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->toBase()->dump(); |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Run a map over each of the items. |
|
106
|
|
|
* |
|
107
|
|
|
* @param callable $callback |
|
108
|
|
|
* @return Collection |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function map(callable $callback) |
|
111
|
|
|
{ |
|
112
|
1 |
|
return $this->toBase()->map($callback); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Run a dictionary map over the items. |
|
117
|
|
|
* |
|
118
|
|
|
* The callback should return an associative array with a single key/value pair. |
|
119
|
|
|
* |
|
120
|
|
|
* @param callable $callback |
|
121
|
|
|
* @return Collection |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function mapToDictionary(callable $callback) |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->toBase()->mapToDictionary($callback); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Run a grouping map over the items. |
|
130
|
|
|
* |
|
131
|
|
|
* The callback should return an associative array with a single key/value pair. |
|
132
|
|
|
* |
|
133
|
|
|
* @param callable $callback |
|
134
|
|
|
* @return Collection |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function mapToGroups(callable $callback) |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $this->toBase()->mapToGroups($callback); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Run an associative map over each of the items. |
|
143
|
|
|
* |
|
144
|
|
|
* The callback should return an associative array with a single key/value pair. |
|
145
|
|
|
* |
|
146
|
|
|
* @param callable $callback |
|
147
|
|
|
* @return Collection |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function mapWithKeys(callable $callback) |
|
150
|
|
|
{ |
|
151
|
1 |
|
return $this->toBase()->mapWithKeys($callback); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Group an associative array by a field or using a callback. |
|
156
|
|
|
* |
|
157
|
|
|
* @param callable|string $groupBy |
|
158
|
|
|
* @param bool $preserveKeys |
|
159
|
|
|
* @return Collection |
|
160
|
|
|
*/ |
|
161
|
2 |
|
public function groupBy($groupBy, $preserveKeys = false) |
|
162
|
|
|
{ |
|
163
|
2 |
|
return $this->toBase()->groupBy($groupBy, $preserveKeys); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get the keys of the collection items. |
|
168
|
|
|
* |
|
169
|
|
|
* @return Collection |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function keys() |
|
172
|
|
|
{ |
|
173
|
1 |
|
return $this->toBase()->keys(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set the item at a given offset. |
|
178
|
|
|
* |
|
179
|
|
|
* @param mixed $key |
|
180
|
|
|
* @param mixed $value |
|
181
|
|
|
*/ |
|
182
|
1 |
|
public function offsetSet($key, $value): void |
|
183
|
|
|
{ |
|
184
|
1 |
|
Assert::isInstanceOf($value, $this->type); |
|
185
|
|
|
|
|
186
|
|
|
parent::offsetSet($key, $value); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|