1
|
|
|
<?php namespace Anomaly\Streams\Platform\Model; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Collection; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class EloquentCollection |
7
|
|
|
* The base eloquent collection used by all our models. |
8
|
|
|
* |
9
|
|
|
* @link http://anomaly.is/streams-platform |
10
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
11
|
|
|
* @author Ryan Thompson <[email protected]> |
12
|
|
|
* @package Anomaly\Streams\Platform\Collection |
13
|
|
|
*/ |
14
|
|
|
class EloquentCollection extends Collection |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Return the item IDs. |
19
|
|
|
* |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function ids() |
23
|
|
|
{ |
24
|
|
|
return $this->lists('id')->all(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return a collection of decorated items. |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
|
|
public function decorated() |
33
|
|
|
{ |
34
|
|
|
$items = []; |
35
|
|
|
|
36
|
|
|
$decorator = app('Robbo\Presenter\Decorator'); |
37
|
|
|
|
38
|
|
|
foreach ($this->items as $item) { |
39
|
|
|
$items[] = $decorator->decorate($item); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return self::make($items); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return shuffled items. |
47
|
|
|
* |
48
|
|
|
* @param int $amount |
|
|
|
|
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function shuffle() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$shuffled = []; |
54
|
|
|
|
55
|
|
|
$keys = array_keys($this->items); |
56
|
|
|
|
57
|
|
|
shuffle($keys); |
58
|
|
|
|
59
|
|
|
foreach ($keys as $key) { |
60
|
|
|
$shuffled[$key] = $this->items[$key]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return new static($shuffled); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Pad to the specified size with a value. |
68
|
|
|
* |
69
|
|
|
* @param $size |
70
|
|
|
* @param null $value |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function pad($size, $value = null) |
74
|
|
|
{ |
75
|
|
|
if ($this->isEmpty()) { |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($value) { |
80
|
|
|
return new static(array_pad($this->items, $size, $value)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
while ($this->count() < $size) { |
84
|
|
|
$this->items = array_merge($this->items, $this->items); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new static($this->items); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Find a model by key. |
92
|
|
|
* |
93
|
|
|
* @param $key |
94
|
|
|
* @param $value |
95
|
|
|
* @return EloquentModel |
96
|
|
|
*/ |
97
|
|
|
public function findBy($key, $value) |
98
|
|
|
{ |
99
|
|
|
return $this->first( |
100
|
|
|
function ($index, $entry) use ($key, $value) { |
101
|
|
|
return $entry->{$key} === $value; |
102
|
|
|
} |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* An alias for slice. |
108
|
|
|
* |
109
|
|
|
* @param $offset |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function skip($offset) |
113
|
|
|
{ |
114
|
|
|
return $this->slice($offset, null, true); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.