|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Analogue\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use Analogue\ORM\Exceptions\MappingException; |
|
6
|
|
|
use Analogue\ORM\System\Manager; |
|
7
|
|
|
use Analogue\ORM\System\Wrappers\Factory; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
class EntityCollection extends Collection |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Wrapper Factory. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \Analogue\ORM\System\Wrappers\Factory |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $factory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* EntityCollection constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param array|null $entities |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(array $entities = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->factory = new Factory(); |
|
28
|
|
|
|
|
29
|
|
|
parent::__construct($entities); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Find an entity in the collection by key. |
|
34
|
|
|
* |
|
35
|
|
|
* @param mixed $key |
|
36
|
|
|
* @param mixed $default |
|
37
|
|
|
* |
|
38
|
|
|
* @throws MappingException |
|
39
|
|
|
* |
|
40
|
|
|
* @return \Analogue\ORM\Entity |
|
41
|
|
|
*/ |
|
42
|
|
|
public function find($key, $default = null) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($key instanceof Mappable) { |
|
45
|
|
|
$key = $this->getEntityKey($key); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return array_first($this->items, function ($entity, $itemKey) use ($key) { |
|
|
|
|
|
|
49
|
|
|
return $this->getEntityKey($entity) == $key; |
|
50
|
|
|
}, $default); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add an entity to the collection. |
|
55
|
|
|
* |
|
56
|
|
|
* @param Mappable $entity |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function add($entity) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->push($entity); |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Remove an entity from the collection. |
|
69
|
|
|
* |
|
70
|
|
|
* @param $entity |
|
71
|
|
|
* |
|
72
|
|
|
* @throws MappingException |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function remove($entity) |
|
77
|
|
|
{ |
|
78
|
|
|
$key = $this->getEntityKey($entity); |
|
79
|
|
|
|
|
80
|
|
|
return $this->pull($key); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Push an item onto the end of the collection. |
|
85
|
|
|
* |
|
86
|
|
|
* @param mixed $value |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function push($value) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->offsetSet(null, $value); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Put an item in the collection by key. |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed $key |
|
99
|
|
|
* @param mixed $value |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function put($key, $value) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->offsetSet($key, $value); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Set the item at a given offset. |
|
110
|
|
|
* |
|
111
|
|
|
* @param mixed $key |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function offsetSet($key, $value) |
|
117
|
|
|
{ |
|
118
|
|
|
if (is_null($key)) { |
|
119
|
|
|
$this->items[] = $value; |
|
120
|
|
|
} else { |
|
121
|
|
|
$this->items[$key] = $value; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Determine if a key exists in the collection. |
|
127
|
|
|
* |
|
128
|
|
|
* @param mixed $key |
|
129
|
|
|
* @param mixed|null $value |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
// public function contains($key, $value = null) |
|
|
|
|
|
|
134
|
|
|
// { |
|
135
|
|
|
// if (func_num_args() == 2) { |
|
|
|
|
|
|
136
|
|
|
// return !$this->where($key, $value)->isEmpty(); |
|
|
|
|
|
|
137
|
|
|
// } |
|
138
|
|
|
|
|
139
|
|
|
// if ($this->useAsCallable($key)) { |
|
|
|
|
|
|
140
|
|
|
// return !is_null($this->first($key)); |
|
|
|
|
|
|
141
|
|
|
// } |
|
142
|
|
|
|
|
143
|
|
|
// return !is_null($this->find($key)); |
|
|
|
|
|
|
144
|
|
|
// } |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Fetch a nested element of the collection. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $key |
|
150
|
|
|
* |
|
151
|
|
|
* @return self |
|
152
|
|
|
*/ |
|
153
|
|
|
public function fetch($key) |
|
154
|
|
|
{ |
|
155
|
|
|
return new static(array_fetch($this->toArray(), $key)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Generic function for returning class.key value pairs. |
|
160
|
|
|
* |
|
161
|
|
|
* @throws MappingException |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
View Code Duplication |
public function getEntityHashes() |
|
|
|
|
|
|
166
|
|
|
{ |
|
167
|
|
|
return array_map(function ($entity) { |
|
168
|
|
|
$class = get_class($entity); |
|
169
|
|
|
|
|
170
|
|
|
$mapper = Manager::getMapper($class); |
|
171
|
|
|
|
|
172
|
|
|
$keyName = $mapper->getEntityMap()->getKeyName(); |
|
173
|
|
|
|
|
174
|
|
|
return $class.'.'.$entity->getEntityAttribute($keyName); |
|
175
|
|
|
}, |
|
176
|
|
|
$this->items); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get a subset of the collection from entity hashes. |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $hashes |
|
183
|
|
|
* |
|
184
|
|
|
* @throws MappingException |
|
185
|
|
|
* |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getSubsetByHashes(array $hashes) |
|
189
|
|
|
{ |
|
190
|
|
|
$subset = []; |
|
191
|
|
|
|
|
192
|
|
|
foreach ($this->items as $item) { |
|
193
|
|
|
$class = get_class($item); |
|
194
|
|
|
|
|
195
|
|
|
$mapper = Manager::getMapper($class); |
|
196
|
|
|
|
|
197
|
|
|
$keyName = $mapper->getEntityMap()->getKeyName(); |
|
198
|
|
|
|
|
199
|
|
|
if (in_array($class.'.'.$item->$keyName, $hashes)) { |
|
200
|
|
|
$subset[] = $item; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $subset; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Merge the collection with the given items. |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $items |
|
211
|
|
|
* |
|
212
|
|
|
* @throws MappingException |
|
213
|
|
|
* |
|
214
|
|
|
* @return self |
|
215
|
|
|
*/ |
|
216
|
|
|
public function merge($items) |
|
217
|
|
|
{ |
|
218
|
|
|
$dictionary = $this->getDictionary(); |
|
219
|
|
|
|
|
220
|
|
|
foreach ($items as $item) { |
|
221
|
|
|
$dictionary[$this->getEntityKey($item)] = $item; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return new static(array_values($dictionary)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Diff the collection with the given items. |
|
229
|
|
|
* |
|
230
|
|
|
* @param \ArrayAccess|array $items |
|
231
|
|
|
* |
|
232
|
|
|
* @return self |
|
233
|
|
|
*/ |
|
234
|
|
View Code Duplication |
public function diff($items) |
|
|
|
|
|
|
235
|
|
|
{ |
|
236
|
|
|
$diff = new static(); |
|
237
|
|
|
|
|
238
|
|
|
$dictionary = $this->getDictionary($items); |
|
239
|
|
|
|
|
240
|
|
|
foreach ($this->items as $item) { |
|
241
|
|
|
if (!isset($dictionary[$this->getEntityKey($item)])) { |
|
242
|
|
|
$diff->add($item); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return $diff; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Intersect the collection with the given items. |
|
251
|
|
|
* |
|
252
|
|
|
* @param \ArrayAccess|array $items |
|
253
|
|
|
* |
|
254
|
|
|
* @throws MappingException |
|
255
|
|
|
* |
|
256
|
|
|
* @return self |
|
257
|
|
|
*/ |
|
258
|
|
View Code Duplication |
public function intersect($items) |
|
|
|
|
|
|
259
|
|
|
{ |
|
260
|
|
|
$intersect = new static(); |
|
261
|
|
|
|
|
262
|
|
|
$dictionary = $this->getDictionary($items); |
|
263
|
|
|
|
|
264
|
|
|
foreach ($this->items as $item) { |
|
265
|
|
|
if (isset($dictionary[$this->getEntityKey($item)])) { |
|
266
|
|
|
$intersect->add($item); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return $intersect; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Returns only the models from the collection with the specified keys. |
|
275
|
|
|
* |
|
276
|
|
|
* @param mixed $keys |
|
277
|
|
|
* |
|
278
|
|
|
* @return self |
|
279
|
|
|
*/ |
|
280
|
|
|
public function only($keys) |
|
281
|
|
|
{ |
|
282
|
|
|
$dictionary = array_only($this->getDictionary(), $keys); |
|
283
|
|
|
|
|
284
|
|
|
return new static(array_values($dictionary)); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Returns all models in the collection except the models with specified keys. |
|
289
|
|
|
* |
|
290
|
|
|
* @param mixed $keys |
|
291
|
|
|
* |
|
292
|
|
|
* @return self |
|
293
|
|
|
*/ |
|
294
|
|
|
public function except($keys) |
|
295
|
|
|
{ |
|
296
|
|
|
$dictionary = array_except($this->getDictionary(), $keys); |
|
297
|
|
|
|
|
298
|
|
|
return new static(array_values($dictionary)); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Get a dictionary keyed by primary keys. |
|
303
|
|
|
* |
|
304
|
|
|
* @param \ArrayAccess|array $items |
|
305
|
|
|
* |
|
306
|
|
|
* @throws MappingException |
|
307
|
|
|
* |
|
308
|
|
|
* @return array |
|
309
|
|
|
*/ |
|
310
|
|
|
public function getDictionary($items = null) |
|
311
|
|
|
{ |
|
312
|
|
|
$items = is_null($items) ? $this->items : $items; |
|
313
|
|
|
|
|
314
|
|
|
$dictionary = []; |
|
315
|
|
|
|
|
316
|
|
|
foreach ($items as $value) { |
|
317
|
|
|
$dictionary[$this->getEntityKey($value)] = $value; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $dictionary; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @throws MappingException |
|
325
|
|
|
* |
|
326
|
|
|
* @return array |
|
327
|
|
|
*/ |
|
328
|
|
|
public function getEntityKeys() |
|
329
|
|
|
{ |
|
330
|
|
|
return array_keys($this->getDictionary()); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @param $entity |
|
335
|
|
|
* |
|
336
|
|
|
* @throws MappingException |
|
337
|
|
|
* |
|
338
|
|
|
* @return mixed |
|
339
|
|
|
*/ |
|
340
|
|
|
protected function getEntityKey($entity) |
|
341
|
|
|
{ |
|
342
|
|
|
$keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName(); |
|
343
|
|
|
|
|
344
|
|
|
$wrapper = $this->factory->make($entity); |
|
345
|
|
|
|
|
346
|
|
|
return $wrapper->getEntityAttribute($keyName); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Get the max value of a given key. |
|
351
|
|
|
* |
|
352
|
|
|
* @param string|null $key |
|
353
|
|
|
* |
|
354
|
|
|
* @throws MappingException |
|
355
|
|
|
* |
|
356
|
|
|
* @return mixed |
|
357
|
|
|
*/ |
|
358
|
|
View Code Duplication |
public function max($key = null) |
|
|
|
|
|
|
359
|
|
|
{ |
|
360
|
|
|
return $this->reduce(function ($result, $item) use ($key) { |
|
361
|
|
|
$wrapper = $this->factory->make($item); |
|
362
|
|
|
|
|
363
|
|
|
return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? |
|
364
|
|
|
$wrapper->getEntityAttribute($key) : $result; |
|
365
|
|
|
}); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Get the min value of a given key. |
|
370
|
|
|
* |
|
371
|
|
|
* @param string|null $key |
|
372
|
|
|
* |
|
373
|
|
|
* @throws MappingException |
|
374
|
|
|
* |
|
375
|
|
|
* @return mixed |
|
376
|
|
|
*/ |
|
377
|
|
View Code Duplication |
public function min($key = null) |
|
|
|
|
|
|
378
|
|
|
{ |
|
379
|
|
|
return $this->reduce(function ($result, $item) use ($key) { |
|
380
|
|
|
$wrapper = $this->factory->make($item); |
|
381
|
|
|
|
|
382
|
|
|
return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) |
|
383
|
|
|
? $wrapper->getEntityAttribute($key) : $result; |
|
384
|
|
|
}); |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Get an array with the values of a given key. |
|
389
|
|
|
* |
|
390
|
|
|
* @param string $value |
|
391
|
|
|
* @param string|null $key |
|
392
|
|
|
* |
|
393
|
|
|
* @return self |
|
394
|
|
|
*/ |
|
395
|
|
|
public function pluck($value, $key = null) |
|
396
|
|
|
{ |
|
397
|
|
|
return new Collection(Arr::pluck($this->items, $value, $key)); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Alias for the "pluck" method. |
|
402
|
|
|
* |
|
403
|
|
|
* @param string $value |
|
404
|
|
|
* @param string|null $key |
|
405
|
|
|
* |
|
406
|
|
|
* @return self |
|
407
|
|
|
*/ |
|
408
|
|
|
public function lists($value, $key = null) |
|
409
|
|
|
{ |
|
410
|
|
|
return $this->pluck($value, $key); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
/** |
|
414
|
|
|
* Return only unique items from the collection. |
|
415
|
|
|
* |
|
416
|
|
|
* @param string|null $key |
|
417
|
|
|
* @param bool $strict |
|
418
|
|
|
* |
|
419
|
|
|
* @throws MappingException |
|
420
|
|
|
* |
|
421
|
|
|
* @return self |
|
422
|
|
|
*/ |
|
423
|
|
|
public function unique($key = null, $strict = false) |
|
424
|
|
|
{ |
|
425
|
|
|
$dictionary = $this->getDictionary(); |
|
426
|
|
|
|
|
427
|
|
|
return new static(array_values($dictionary)); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Get a base Support collection instance from this collection. |
|
432
|
|
|
* |
|
433
|
|
|
* @return \Illuminate\Support\Collection |
|
434
|
|
|
*/ |
|
435
|
|
|
public function toBase() |
|
436
|
|
|
{ |
|
437
|
|
|
return new Collection($this->items); |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
public function toArray() |
|
441
|
|
|
{ |
|
442
|
|
|
return array_values(parent::toArray()); |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/** |
|
446
|
|
|
* Get the collection of items as JSON. |
|
447
|
|
|
* |
|
448
|
|
|
* @param int $options |
|
449
|
|
|
* |
|
450
|
|
|
* @return string |
|
451
|
|
|
*/ |
|
452
|
|
|
public function toJson($options = 0) |
|
453
|
|
|
{ |
|
454
|
|
|
$collection = new Collection(array_values($this->items)); |
|
455
|
|
|
|
|
456
|
|
|
return $collection->toJson($options); |
|
457
|
|
|
} |
|
458
|
|
|
} |
|
459
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.