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
|
<?php namespace Illuminate\Database\Eloquent;
use Illuminate\Support\Collection as BaseCollection;
class Collection extends BaseCollection {
/** * Find a model in the collection by key. * * @param mixed $key * @param mixed $default * @return \Illuminate\Database\Eloquent\Model */ public function find($key, $default = null) { if ($key instanceof Model) { $key = $key->getKey(); }
return array_first($this->items, function($itemKey, $model) use ($key) { return $model->getKey() == $key;
}, $default); }
/** * Load a set of relationships onto the collection. * * @param dynamic $relations * @return \Illuminate\Database\Eloquent\Collection */ public function load($relations) { if (count($this->items) > 0) { if (is_string($relations)) $relations = func_get_args();
$query = $this->first()->newQuery()->with($relations);
$this->items = $query->eagerLoadRelations($this->items); }
return $this; }
/** * Add an item to the collection. * * @param mixed $item * @return \Illuminate\Database\Eloquent\Collection */ public function add($item) { $this->items[] = $item;
return $this; }
/** * Determine if a key exists in the collection. * * @param mixed $key * @return bool */ public function contains($key) { return ! is_null($this->find($key)); }
/** * Fetch a nested element of the collection. * * @param string $key * @return \Illuminate\Support\Collection */ public function fetch($key) { return new static(array_fetch($this->toArray(), $key)); }
/** * Get the max value of a given key. * * @param string $key * @return mixed */ public function max($key) { return $this->reduce(function($result, $item) use ($key) { return (is_null($result) || $item->{$key} > $result) ? $item->{$key} : $result; }); }
/** * Get the min value of a given key. * * @param string $key * @return mixed */ public function min($key) { return $this->reduce(function($result, $item) use ($key) { return (is_null($result) || $item->{$key} < $result) ? $item->{$key} : $result; }); }
/** * Get the array of primary keys * * @return array */ public function modelKeys() { return array_map(function($m) { return $m->getKey(); }, $this->items); }
/** * Merge the collection with the given items. * * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items * @return \Illuminate\Support\Collection */ public function merge($collection) { $dictionary = $this->getDictionary();
foreach ($collection as $item) { $dictionary[$item->getKey()] = $item; }
return new static(array_values($dictionary)); }
/** * Diff the collection with the given items. * * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items * @return \Illuminate\Support\Collection */ public function diff($collection) { $diff = new static;
$dictionary = $this->getDictionary($collection);
foreach ($this->items as $item) { if ( ! isset($dictionary[$item->getKey()])) { $diff->add($item); } }
return $diff; }
/** * Intersect the collection with the given items. * * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items * @return \Illuminate\Support\Collection */ public function intersect($collection) { $intersect = new static;
$dictionary = $this->getDictionary($collection);
foreach ($this->items as $item) { if (isset($dictionary[$item->getKey()])) { $intersect->add($item); } }
return $intersect; }
/** * Return only unique items from the collection. * * @return \Illuminate\Support\Collection */ public function unique() { $dictionary = $this->getDictionary();
return new static(array_values($dictionary)); }
/** * Returns only the models from the collection with the specified keys. * * @param mixed $keys * @return \Illuminate\Support\Collection */ public function only($keys) { $dictionary = array_only($this->getDictionary($this), $keys);
return new static(array_values($dictionary)); }
/** * Returns all models in the collection except the models with specified keys. * * @param mixed $keys * @return \Illuminate\Support\Collection */ public function except($keys) { $dictionary = array_except($this->getDictionary($this), $keys);
return new static(array_values($dictionary)); }
/** * Get a dictionary keyed by primary keys. * * @param \Illuminate\Support\Collection $collection * @return array */ public function getDictionary($collection = null) { $collection = $collection ?: $this;
$dictionary = array();
foreach ($collection as $value) { $dictionary[$value->getKey()] = $value; }
return $dictionary; }
/** * Get a base Support collection instance from this collection. * * @return \Illuminate\Support\Collection */ public function toBase() { return new BaseCollection($this->items); }
}
|