1
|
|
|
<?php namespace Anomaly\Streams\Platform\Model; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Support\Decorator; |
4
|
|
|
use Anomaly\Streams\Platform\Traits\Hookable; |
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class EloquentCollection |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class EloquentCollection extends Collection |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
use Hookable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Return the item IDs. |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function ids() |
25
|
|
|
{ |
26
|
|
|
return $this->pluck('id')->all(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return a collection of decorated items. |
31
|
|
|
* |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
|
|
public function decorated() |
35
|
|
|
{ |
36
|
|
|
$items = []; |
37
|
|
|
|
38
|
|
|
$decorator = app('Robbo\Presenter\Decorator'); |
39
|
|
|
|
40
|
|
|
foreach ($this->items as $item) { |
41
|
|
|
$items[] = $decorator->decorate($item); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return self::make($items); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Pad to the specified size with a value. |
49
|
|
|
* |
50
|
|
|
* @param $size |
51
|
|
|
* @param null $value |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function pad($size, $value = null) |
55
|
|
|
{ |
56
|
|
|
if ($this->isEmpty()) { |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($value) { |
61
|
|
|
return new static(array_pad($this->items, $size, $value)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
while ($this->count() < $size) { |
65
|
|
|
$this->items = array_merge($this->items, $this->items); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return new static($this->items); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Find a model by key. |
73
|
|
|
* |
74
|
|
|
* @param $key |
75
|
|
|
* @param $value |
76
|
|
|
* @return EloquentModel |
77
|
|
|
*/ |
78
|
|
|
public function findBy($key, $value) |
79
|
|
|
{ |
80
|
|
|
return $this->first( |
81
|
|
|
function ($entry) use ($key, $value) { |
82
|
|
|
return $entry->{$key} === $value; |
83
|
|
|
} |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* An alias for slice. |
89
|
|
|
* |
90
|
|
|
* @param $offset |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function skip($offset) |
94
|
|
|
{ |
95
|
|
|
return $this->slice($offset, null, true); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return undecorated items. |
100
|
|
|
* |
101
|
|
|
* @return static|$this |
102
|
|
|
*/ |
103
|
|
|
public function undecorate() |
104
|
|
|
{ |
105
|
|
|
return new static((new Decorator())->undecorate($this->items)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Map to get. |
110
|
|
|
* |
111
|
|
|
* @param $name |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
public function __get($name) |
115
|
|
|
{ |
116
|
|
|
if ($this->hasHook($hook = 'get_' . $name)) { |
117
|
|
|
return $this->call($hook, []); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($this->has($name)) { |
121
|
|
|
return $this->get($name); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return call_user_func([$this, camel_case($name)]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Map to get. |
129
|
|
|
* |
130
|
|
|
* @param string $method |
131
|
|
|
* @param array $parameters |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function __call($method, $parameters) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
if ($this->hasHook($hook = snake_case($method))) { |
136
|
|
|
return $this->call($hook, $parameters); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->get($method); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.